Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string path = "\\192.168.0.2\delete\test.dat
- <configuration>
- <system.web>
- <identity impersonate="true" userName="192.168.0.2Admin"
- password="pass123"//>
- </system.web>
- </configuration>
- public class NetworkManager : IDisposable
- {
- private readonly string _networkName;
- public NetworkManager(string networkName)
- {
- _networkName = networkName;
- NetResource netResource = new NetResource
- {
- Scope = ResourceScope.GlobalNetwork,
- ResourceType = ResourceType.Any,
- DisplayType = ResourceDisplayType.Directory,
- RemoteName = networkName
- };
- int result = WNetAddConnection2(netResource, "ВАШ ПАРОЛЬ", "ВАШ ЛОГИН", 0);
- if (result != 0)
- {
- throw new Win32Exception(result);
- }
- }
- ~NetworkManager()
- {
- Dispose(false);
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- protected virtual void Dispose(bool disposing)
- {
- WNetCancelConnection2(_networkName, 0, true);
- }
- [DllImport("mpr.dll")]
- private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
- [DllImport("mpr.dll")]
- private static extern int WNetCancelConnection2(string name, int flags, bool force);
- }
- using (NetworkManager network = new NetworkManager(@"\192.168.0.2"))
- {
- // ваш код с подключением
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement