public PersistentConnection(string networkName, NetworkCredential credentials) { _networkName = networkName; var netResource = new NetResource() { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplaytype.Share, RemoteName = networkName }; var userName = string.IsNullOrEmpty(credentials.Domain) ? credentials.UserName : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName); var result = WNetAddConnection2( netResource, credentials.Password, userName, 0); CleanupLog.WriteLine($"Connection Result: {result}"); if (result != 0 && result != 1219) { throw new Win32Exception(result, "Error connecting to remote share"); } }
static void HandleException(Exception e) { //Handle your Exception here CleanupLog.WriteLine($"ERROR! {e}"); }