첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

TestProject.zip
0.01MB

▶ ResourceScope.cs

namespace TestProject
{
    /// <summary>
    /// 리소스 범위
    /// </summary>
    public enum ResourceScope : int
    {
        /// <summary>
        /// 연결시
        /// </summary>
        Connected = 1,

        /// <summary>
        /// 글로벌 네트워크
        /// </summary>
        GlobalNetwork,

        /// <summary>
        /// 기억시
        /// </summary>
        Remembered,

        /// <summary>
        /// 최근
        /// </summary>
        Recent,

        /// <summary>
        /// 컨텍스트
        /// </summary>
        Context
    };
}

 

728x90

 

▶ ResourceType.cs

namespace TestProject
{
    /// <summary>
    /// 리소스 타입
    /// </summary>
    public enum ResourceType : int
    {
        /// <summary>
        /// 아무거나
        /// </summary>
        Any = 0,

        /// <summary>
        /// 디스크
        /// </summary>
        Disk = 1,

        /// <summary>
        /// 인쇄
        /// </summary>
        Print = 2,

        /// <summary>
        /// 예약
        /// </summary>
        Reserved = 8,
    }
}

 

300x250

 

▶ ResourceDisplayType.cs

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

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

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

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
    }
}
728x90
그리드형(광고전용)
Posted by icodebroker
,