private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
/// <summary> /// 네트워크 경로에 대한 연결 /// </summary> /// <param name="networkName"></param> /// <param name="credentials"></param> /// <exception cref="System.ComponentModel.Win32Exception">이 Credential 을 가지고 있는 프로그램이 아직 열려 있으면 아래 오류가 나타나더라...</exception> public NetworkConnection(string networkName, NetworkCredential credentials) { _networkName = networkName; var netResource = new NetResource() { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplaytype.Share, RemoteName = networkName }; int result = WNetAddConnection2( netResource, credentials.Password, (credentials.Domain.Length > 0) ? credentials.Domain + "\\" + credentials.UserName : credentials.UserName, 0); if (result != 0) { throw new System.ComponentModel.Win32Exception(result, "Error connecting to remote share"); } }