공유폴더에 접근해 로그인하고 파일을 복사하는 방법을 남깁니다.
WNetUseConnection를 이용해 해당 공유폴더에 접속해서 로그인 하면 됩니다. 
그런데 그냥 로그인하고 파일 복사나 수정 / 삭제를 한뒤에 다시 연결을 끊어줘야 하는데 

그것이 마지막에 있는 WNetCancelConnection2A 입니다. 
되도록 작업이 끝난 후에는 닫아주는게 좋을거 같습니다. 

그리고 아래 코드를 이용해 연결 되었다면 File이나 FileInfo를 이용하면 됩니다.
01class SharedAPI
02    {
03        // 구조체 선언
04        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
05        public struct NETRESOURCE
06        {
07            public uint dwScope;
08            public uint dwType;
09            public uint dwDisplayType;
10            public uint dwUsage;
11            public string lpLocalName;
12            public string lpRemoteName;
13            public string lpComment;
14            public string lpProvider;
15        }
16 
17        // API 함수 선언
18        [DllImport("mpr.dll", CharSet = CharSet.Auto)]
19        public static extern int WNetUseConnection(
20                    IntPtr hwndOwner,
21                    [MarshalAs(UnmanagedType.Struct)] ref NETRESOURCE lpNetResource,
22                    string lpPassword,
23                    string lpUserID,
24                    uint dwFlags,
25                    StringBuilder lpAccessName,
26                    ref int lpBufferSize,
27                    out uint lpResult);
28 
29        // API 함수 선언 (공유해제)
30        [DllImport("mpr.dll", EntryPoint = "WNetCancelConnection2", CharSet = CharSet.Auto)]
31        public static extern int WNetCancelConnection2A(string lpName, int dwFlags, int fForce);
32 
33        // 공유연결
34        public int ConnectRemoteServer(string server)
35        {
36            int capacity = 64;
37            uint resultFlags = 0;
38            uint flags = 0;
39            System.Text.StringBuilder sb = new System.Text.StringBuilder(capacity);
40            NETRESOURCE ns = new NETRESOURCE();
41            ns.dwType = 1;              // 공유 디스크
42            ns.lpLocalName = null;   // 로컬 드라이브 지정하지 않음
43            ns.lpRemoteName = server;
44            ns.lpProvider = null;
45            int result = 0;
46            if (server == @"\\10.144.70.120\d$")
47            {
48                result = WNetUseConnection(IntPtr.Zero, ref ns, "djfrmfpdl", "administrator", flags,
49                                            sb, ref capacity, out resultFlags);
50            }
51            else
52            {
53                result = WNetUseConnection(IntPtr.Zero, ref ns, "PASSWORD, "LOGINID", flags,
54                                                sb, ref capacity, out resultFlags);
55            }
56            return result;
57        }
58 
59        // 공유해제
60        public void CencelRemoteServer(string server)
61        {
62            WNetCancelConnection2A(server, 1, 0);
63        }
64    }
Posted by 아르다