개발관련/C#

네트워크 공유 폴더 접근

Diademata 2018. 5. 9. 09:49
반응형

https://code.msdn.microsoft.com/windowsapps/Connecting-to-Remote-f69b9346/sourcecode?fileId=25381&pathId=564469262


NetworkConnection.cs


using System;

using System.IO;

using System.Net;

using System.Runtime.InteropServices;


namespace Connect.Base

{

    public class NetworkConnection : IDisposable

    {

        protected string _connectString;

        protected NetworkCredential _credential;

        public NetworkConnection(string connectString, NetworkCredential credential)

        {

            _connectString = connectString;

            _credential = credential;

        }

        public void Open()

        {

            var netResource = new NetResource

            {

                Scope = ResourceScope.GlobalNetwork,

                ResourceType = ResourceType.Disk,

                DisplayType = ResourceDisplaytype.Share,

                RemoteName = _connectString

            };


            var result = WNetAddConnection2(netResource, _credential.Password, _credential.UserName, 0);


            if (result != 0)

            {

                throw new IOException("Error connecting to remote share", result);

            }

        }

        public string ConnectString { get => _connectString; }

        public void Close()

        {

            WNetCancelConnection2(_connectString, 0, true);

        }

        ~NetworkConnection()

        {

            Dispose(false);

        }

        public void Dispose()

        {

            Dispose(true);

        }

        protected void Dispose(bool disposing)

        {

            WNetCancelConnection2(_connectString, 0, true);

            GC.SuppressFinalize(this);

        }

        [DllImport("mpr.dll")]

        private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);

        [DllImport("mpr.dll")]

        private static extern int WNetCancelConnection2(string name, int flags, bool force);


        [StructLayout(LayoutKind.Sequential)]

        private class NetResource

        {

            public ResourceScope Scope;

            public ResourceType ResourceType;

            public ResourceDisplaytype DisplayType;

            public int Usage;

            public string LocalName;

            public string RemoteName;

            public string Comment;

            public string Provider;

        }


        private enum ResourceScope : int

        {

            Connected = 1,

            GlobalNetwork,

            Remembered,

            Recent,

            Context

        }


        private enum ResourceType : int

        {

            Any = 0,

            Disk = 1,

            Print = 2,

            Reserved = 8

        }


        private enum ResourceDisplaytype : int

        {

            Generic = 0x0,

            Domain = 0x1,

            Server = 0x2,

            Share = 0x3,

            File = 0x4,

            Group = 0x5,

            Network = 0x6,

            Root = 0x7,

            Shareadmin = 0x8,

            Directory = 0x9,

            Tree = 0xa,

            Ndscontainer = 0xb

        }

    }

}


IBase 추상 클래스


RDBMS(MSSQL, ORACLE, MYSQL)용과 파일 네트워크 연결용 클래스를 범용하게 사용하기 위해


Init, Connect, Dispose 메소드를 가짐


FileConnection.cs


using Connect.Base;

using System.Net;


namespace Connect

{

    public class FileConnection : IBase

    {

        NetworkConnection _netConnect;

        public override void Init(string connectString, string account = null, string password = null)

        {

            NetworkCredential _credential = new NetworkCredential()

            {

                UserName = account,

                Password = password

            };

            _netConnect = new NetworkConnection(connectString, _credential);

        }

        public string ConnectString { get => _netConnect.ConnectString; }

        public override IBase Connect()

        {

            _netConnect.Open();

            return this;

        }

        public override void Dispose()

        {

            _netConnect.Close();

            _netConnect.Dispose();

        }

    }

}


사용예제


_targetContext.Init(... , ...);


using (var conn = _targetContext.Connect())

{

   var filePath = Directory.GetFiles((connect as FileConnection).ConnectString)              

}




반응형

'개발관련 > C#' 카테고리의 다른 글

sql compact 4.0 설정  (2) 2018.07.30
SignalR MessageFormat 및 구동 시퀸스  (0) 2018.07.14
Winform Control 안쪽 여백 제거  (0) 2018.04.08
쿼드 트리  (0) 2018.03.28
DLL 동적 로딩  (0) 2017.12.18