using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel;
public class YourClass {
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref NetResource pstNetRes, string psPassword, string psUsername, int piFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2A(string psName, int piFlags, int pfForce);
[StructLayout(LayoutKind.Sequential)]
private struct NetResource {
public int iScope;
public int iType;
public int iDisplayType;
public int iUsage;
public string sLocalName;
public string sRemoteName;
public string sComment;
public string sProvider;
}
private const int RESOURCETYPE_DISK = 0x1;
private void LoginToShare(string serverName, string shareName, string user, string password) {
string destinationDirectory = string.Format(@"\\{0}\{1}", serverName, shareName);
NetResource nr = new NetResource();
nr.iScope = 2;
nr.iType = RESOURCETYPE_DISK;
nr.iDisplayType = 3;
nr.iUsage = 1;
nr.sRemoteName = destinationDirectory;
nr.sLocalName = null;
int flags = 0;
int rc = WNetAddConnection2A(ref nr, password, user, flags);
if (rc != 0) throw new Win32Exception(rc);
}
private void LogoutFromShare(string serverName, string shareName) {
string destinationDirectory = string.Format(@"\\{0}\{1}", serverName, shareName);
int flags = 0;
int rc = WNetCancelConnection2A(destinationDirectory, flags, Convert.ToInt32(false));
}
c#을 이용하여 공유폴더 접근하는 방법
[원문] http://www.nullskull.com/q/10116970/accessing-shared-folder-on-a-network-using-c-code.asp
'C#' 카테고리의 다른 글
c# ,System.Environment.SpecialFolder 윈도우 특정 폴더 PATH 구하기 (0) | 2021.03.03 |
---|---|
C# .NET Global exception handler in console application (0) | 2020.10.15 |
c# mediaplayer 재생 하기 , c# play mp3 by media player (0) | 2020.10.15 |
c# cefsharp filedownload 구현 (0) | 2020.10.15 |