■ 네트워크 드라이브를 사용하는 방법을 보여준다.
▶ ResourceScope.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
namespace TestProject { /// <summary> /// 리소스 범위 /// </summary> public enum ResourceScope : int { /// <summary> /// 연결시 /// </summary> Connected = 1, /// <summary> /// 글로벌 네트워크 /// </summary> GlobalNetwork, /// <summary> /// 기억시 /// </summary> Remembered, /// <summary> /// 최근 /// </summary> Recent, /// <summary> /// 컨텍스트 /// </summary> Context }; } |
▶ ResourceType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
namespace TestProject { /// <summary> /// 리소스 타입 /// </summary> public enum ResourceType : int { /// <summary> /// 아무거나 /// </summary> Any = 0, /// <summary> /// 디스크 /// </summary> Disk = 1, /// <summary> /// 인쇄 /// </summary> Print = 2, /// <summary> /// 예약 /// </summary> Reserved = 8, } } |
▶ ResourceDisplayType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
namespace TestProject { /// <summary> /// 리소스 디스플레이 타입 /// </summary> public enum ResourceDisplayType : int { /// <summary> /// 일반 /// </summary> Generic = 0x0, /// <summary> /// 도메인 /// </summary> Domain = 0x01, /// <summary> /// 서버 /// </summary> Server = 0x02, /// <summary> /// 공유 /// </summary> Share = 0x03, /// <summary> /// 파일 /// </summary> File = 0x04, /// <summary> /// 그룹 /// </summary> Group = 0x05, /// <summary> /// 네트워크 /// </summary> Network = 0x06, /// <summary> /// 루트 /// </summary> Root = 0x07, /// <summary> /// 공유 관리 /// </summary> ShareAdmin = 0x08, /// <summary> /// 디렉토리 /// </summary> Directory = 0x09, /// <summary> /// 트리 /// </summary> Tree = 0x0a, /// <summary> /// NDS 컨테이너 /// </summary> NDSContainer = 0x0b } } |
▶ NetworkResource.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 네트워크 리소스 /// </summary> [StructLayout(LayoutKind.Sequential)] public class NetworkResource { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 범위 /// </summary> public ResourceScope Scope; /// <summary> /// 리소스 타입 /// </summary> public ResourceType ResourceType; /// <summary> /// 디스플레이 타입 /// </summary> public ResourceDisplayType DisplayType; /// <summary> /// 용법 /// </summary> public int Usage; /// <summary> /// 지역명 /// </summary> public string LocalName; /// <summary> /// 원격명 /// </summary> public string RemoteName; /// <summary> /// 주석 /// </summary> public string Comment; /// <summary> /// 공급자 /// </summary> public string Provider; #endregion } } |
▶ NetworkConnection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
using System; using System.ComponentModel; using System.Net; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 네트워크 연결 /// </summary> public class NetworkConnection : IDisposable { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region WNET 연결 추가하기 2 - WNetAddConnection2(networkResource, password, userName, flag) /// <summary> /// WNET 연결 추가하기 2 /// </summary> /// <param name="networkResource">네트워크 리소스</param> /// <param name="password">패스워드</param> /// <param name="userName">사용자명</param> /// <param name="flag">플래그</param> /// <returns>처리 결과</returns> [DllImport("mpr")] private static extern int WNetAddConnection2(NetworkResource networkResource, string password, string userName, int flag); #endregion #region WNET 연결 취소하기 2 - WNetCancelConnection2(name, flag, force) /// <summary> /// WNET 연결 취소하기 2 /// </summary> /// <param name="name">네트워크명</param> /// <param name="flag">플래그</param> /// <param name="force">강제 여부</param> /// <returns>처리 결과</returns> [DllImport("mpr")] private static extern int WNetCancelConnection2(string name, int flag, bool force); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 로컬명 /// </summary> private string localName; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - NetworkConnection(localName, remoteName, credential) /// <summary> /// 생성자 /// </summary> /// <param name="localName">로컬명</param> /// <param name="remoteName">원격명</param> /// <param name="credential">자격 증명</param> public NetworkConnection(string localName, string remoteName, NetworkCredential credential) { this.localName = localName; NetworkResource networkResource = new NetworkResource() { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplayType.Share, LocalName = localName, RemoteName = remoteName }; string userName; if(string.IsNullOrEmpty(credential.Domain)) { userName = credential.UserName; } else { userName = string.Format(@"{0}\{1}", credential.Domain, credential.UserName); } int result = WNetAddConnection2(networkResource, credential.Password, userName, 0); if(result != 0) { throw new Win32Exception(result); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Destructor #region 소멸자 - ~NetworkConnection() /// <summary> /// 소멸자 /// </summary> ~NetworkConnection() { Dispose(false); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 리소스 해제하기 - Dispose() /// <summary> /// 리소스 해제하기 /// </summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 리소스 해제하기 - Dispose(disposing) /// <summary> /// 리소스 해제하기 /// </summary> /// <param name="disposing">해제 여부</param> protected virtual void Dispose(bool disposing) { WNetCancelConnection2(this.localName, 0, true); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Net; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { NetworkCredential credential = new NetworkCredential("admin", "1234"); using(NetworkConnection connection = new NetworkConnection("Z:", @"\\PALACE\test", credential)) { Console.ReadKey(true); } } #endregion } } |