Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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. [StructLayout(LayoutKind.Sequential)]
  58. public class NetResource
  59. {
  60. public ResourceScope Scope;
  61. public ResourceType ResourceType;
  62. public ResourceDisplayType DisplayType;
  63. public int Usage;
  64. public string LocalName;
  65. public string RemoteName;
  66. public string Comment;
  67. public string Provider;
  68. }
  69.  
  70. public enum ResourceScope
  71. {
  72. Connected = 1,
  73. GlobalNetwork,
  74. Remembered,
  75. Recent,
  76. Context
  77. }
  78.  
  79. public enum ResourceType
  80. {
  81. Any = 0,
  82. Disk = 1,
  83. Print = 2,
  84. Reserved = 8,
  85. }
  86.  
  87. public enum ResourceDisplayType
  88. {
  89. Generic = 0x0,
  90. Domain = 0x01,
  91. Server = 0x02,
  92. Share = 0x03,
  93. File = 0x04,
  94. Group = 0x05,
  95. Network = 0x06,
  96. Root = 0x07,
  97. Shareadmin = 0x08,
  98. Directory = 0x09,
  99. Tree = 0x0a,
  100. Ndscontainer = 0x0b
  101. }
  102.  
  103. using (NetworkManager network = new NetworkManager(@"\192.168.0.2"))
  104. {
  105. // ваш код с подключением
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement