일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 센티넬
- ASP.NET
- ipod
- High Availability
- replication
- DotNetOpenAuth
- 기어스 오브 워
- iPod Touch
- 닷넷
- 네아로
- mmbot
- 레디스
- sentinel
- XBOX360
- DSM6
- AirComic
- haproxy
- 복제
- 개발
- 고가용성
- TeamCity
- redis
- Gears of War
- .NET
- c#
- Cover Guru
- gitlab
- NoSQL
- 커버구루
- Keepalived
- Today
- Total
Once in a Lifetime
ASP.NET :: 네트워크 드라이브 연결하기 본문
Windows Networking (WNET) 이라는 네트워크 드라이브 관련 API 를 사용하면 손쉽게 네트워크 드라이브로 접속해서 원하는 작업을 할 수 있다.
서버의 공유폴더에 액세스해서 작업하는 API로, 본 포스트에서는 WNetUseConnection 를 사용하는 방법을 소개하도록 하겠다. 두둥~
Step I :: NETRESOURCE Structure
먼저 NETRESOURCE 라는 구조체를 선언한다.
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;
}
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
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 형으로 변경했다.
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);
(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
이제 선언된 메서드를 호출해서 사용하기만 하면 된다. 아래의 예제와 같이 호출도록 한다.
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);
Dim lngRet As Integer
Dim Result As Integer
dim intFlag As Integer = 0
Dim strBuffer As String
Dim intBuffLen As Integer
With NETR
.dwType = RESOURCETYPE_DISK
.lpRemoteName = "\\filesvr\shared"
.lpProvider = ""
End With
lngRet = WNetUseConnection(0, NETR, "userpw", "userid", intFlag, strBuffer, intBuffLen, Result)