Once in a Lifetime

ASP.NET :: 네트워크 드라이브 연결하기 본문

Develope Diary

ASP.NET :: 네트워크 드라이브 연결하기

riceworld 2007. 3. 26. 13:02
가장 최근에 작업했던 프로젝트에서 FTP(원격지) 에서 네트워크 드라이브로 파일을 다운로드해야 할 일이 있어서 구글링해서 건진 정보.

Windows Networking (WNET) 이라는 네트워크 드라이브 관련 API 를 사용하면 손쉽게 네트워크 드라이브로 접속해서 원하는 작업을 할 수 있다.

서버의 공유폴더에 액세스해서 작업하는 API로, 본 포스트에서는 WNetUseConnection 를 사용하는 방법을 소개하도록 하겠다. 두둥~

Step I :: NETRESOURCE Structure

먼저 NETRESOURCE 라는 구조체를 선언한다.

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct NETRESOURCE
{
    public uint dwScope;
    public uint dwType;
    public uint dwDisplayType;
    public uint dwUsage;
    public string lpLocalName;
    public string lpRemoteName;
    public string lpComment;
    public string lpProvider;
}
[ C# ]

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
    Public Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public lpLocalName As String
        Public lpRemoteName As String
        Public lpComment As String
        Public lpProvider As String
    End Structure

[ VB.NET ]



Step II :: CONSTANT Declare (Only VB.NET)

상수에 대한 자세한 설명은 생략하겠다. 자세한 사항은 MSDN 참고.

#Region "Remote Connect API"
    Private Const NO_ERROR = 0
    Private Const CONNECT_UPDATE_PROFILE = &H1
    Private Const RESOURCETYPE_DISK = &H1
    Private Const RESOURCETYPE_PRINT = &H2
    Private Const RESOURCETYPE_ANY = &H0
    Private Const RESOURCE_CONNECTED = &H1
    Private Const RESOURCE_REMEMBERED = &H3
    Private Const RESOURCE_GLOBALNET = &H2
    Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
    Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
    Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
    Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Private Const RESOURCEUSAGE_CONNECTABLE = &H1
    Private Const RESOURCEUSAGE_CONTAINER = &H2

    ' Error Constants:
    Private Const ERROR_ACCESS_DENIED = 5&
    Private Const ERROR_ALREADY_ASSIGNED = 85&
    Private Const ERROR_BAD_DEV_TYPE = 66&
    Private Const ERROR_BAD_DEVICE = 1200&
    Private Const ERROR_BAD_NET_NAME = 67&
    Private Const ERROR_BAD_PROFILE = 1206&
    Private Const ERROR_BAD_PROVIDER = 1204&
    Private Const ERROR_BUSY = 170&
    Private Const ERROR_CANCELLED = 1223&
    Private Const ERROR_CANNOT_OPEN_PROFILE = 1205&
    Private Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
    Private Const ERROR_EXTENDED_ERROR = 1208&
    Private Const ERROR_INVALID_PASSWORD = 86&
    Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
    Private Const ERROR_NO_NET_OR_BAD_SERVER = 53&
    Private Const ERROR_BAD_USER_OR_PASSWORD = 1326&


Step III :: WNetUseConnection Method Declare

여타의 API 와 마찬가지로 메서드를 선언해 준다. C# 에서 Unit 형은 VB.NET 에서 지원하지 않으므로, Integer 형으로 변경했다.

[DllImport("mpr.dll", CharSet=CharSet.Auto)]
public static extern int WNetUseConnection(
            IntPtr hwndOwner,
            [MarshalAs(UnmanagedType.Struct)] ref NETRESOURCE lpNetResource,
            string lpPassword,
            string lpUserID,
            uint dwFlags,
            StringBuilder lpAccessName,
            ref int lpBufferSize,
            out uint lpResult);
[ C# ]

    Declare Auto Function WNetUseConnection Lib "mpr.dll" _
            (ByVal hwndOwner As Integer, _
             ByRef lpNetResource As NETRESOURCE, _
             ByVal lpPassword As String, _
             ByVal lpUserID As String, _
             ByVal dwFlags As Integer, _
             ByVal lpAccessName As String, _
             ByRef lpBufferSize As Integer, _
             ByRef lpResult As Integer) As Integer
[ VB.NET ]

Step IV :: WNetUseConnection Method Call

이제 선언된 메서드를 호출해서 사용하기만 하면 된다. 아래의 예제와 같이 호출도록 한다.

int capacity = 64;
uint resultFlags = 0;
uint flags = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder(capacity);
NETRESOURCE ns = new NETRESOURCE();
ns.dwType = 1;           // 공유 디스크
ns.lpLocalName = null;   // 로컬 드라이브 지정하지 않음
ns.lpRemoteName = @"\\filesvr\shared";
ns.lpProvider = null;

int result = WNetUseConnection(IntPtr.Zero, ref ns, "password", "userid", flags,
                                sb, ref capacity, out resultFlags);
[ C# ]

Dim NETR As NETRESOURCE
Dim lngRet As Integer
Dim Result As Integer
dim intFlag As Integer = 0
Dim strBuffer As String
Dim intBuffLen As Integer
strBuffer = Space(64) : intBuffLen = Len(buffer)
With NETR
     .dwType = RESOURCETYPE_DISK
     .lpRemoteName = "\\filesvr\shared"
     .lpProvider = ""
End With
lngRet = WNetUseConnection(0, NETR, "userpw", "userid", intFlag, strBuffer, intBuffLen, Result)
[ VB.NET ]


이 정도의 설명이면 충분히 WNetUseConnection 을 사용하여, 네트워크 드라이브에 연결하여 하고자 하는 작업을 원만히 수행할 수 있으리라 생각한다. 궁금증이 있다면 가장 최적의 답변자는 언제나 MSDN 이 될것임을 잊지말자. ^^;
Comments