/// <summary> /// Initializes a new instance of the <see cref="NetworkConnection"/> class. /// </summary> /// <param name="networkName"> /// The full path of the network share. /// </param> /// <param name="credentials"> /// The credentials to use when connecting to the network share. /// </param> public NetworkConnection(string networkName, NetworkCredential credentials) { try { _networkName = networkName; var netResource = new NetResource { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplaytype.Share, RemoteName = networkName.TrimEnd('\\') }; var result = WNetAddConnection2( netResource, credentials.Password, credentials.UserName, 0); Logging.LogErrors(ConfigurationValues.ErrorLogPath, "The result is: " + result.ToString()); if (result != 0) { throw new Win32Exception(result); } } catch (Win32Exception er) { Logging.LogErrors(ConfigurationValues.ErrorLogPath, "User credential is: " + credentials.UserName + "error is: " + er.ToString()); } }
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);