Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. string path = "\\192.168.0.2\delete\test.dat
  2.  
  3. <configuration>
  4. <system.web>
  5. <identity impersonate="true" userName="192.168.0.2Admin"
  6. password="pass123"//>
  7. </system.web>
  8. </configuration>
  9.  
  10. public class NetworkManager : IDisposable
  11. {
  12. private readonly string _networkName;
  13.  
  14. public NetworkManager(string networkName)
  15. {
  16. _networkName = networkName;
  17.  
  18. NetResource netResource = new NetResource
  19. {
  20. Scope = ResourceScope.GlobalNetwork,
  21. ResourceType = ResourceType.Any,
  22. DisplayType = ResourceDisplayType.Directory,
  23. RemoteName = networkName
  24. };
  25.  
  26. int result = WNetAddConnection2(netResource, "ВАШ ПАРОЛЬ", "ВАШ ЛОГИН", 0);
  27.  
  28. if (result != 0)
  29. {
  30. throw new Win32Exception(result);
  31. }
  32. }
  33.  
  34. ~NetworkManager()
  35. {
  36. Dispose(false);
  37. }
  38.  
  39. public void Dispose()
  40. {
  41. Dispose(true);
  42. GC.SuppressFinalize(this);
  43. }
  44.  
  45. protected virtual void Dispose(bool disposing)
  46. {
  47. WNetCancelConnection2(_networkName, 0, true);
  48. }
  49.  
  50. [DllImport("mpr.dll")]
  51. private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
  52.  
  53. [DllImport("mpr.dll")]
  54. private static extern int WNetCancelConnection2(string name, int flags, bool force);
  55. }
  56.  
  57. using (NetworkManager network = new NetworkManager(@"\192.168.0.2"))
  58. {
  59. // ваш код с подключением
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement