可以尝试用一下函数实现,要是没权限就用下面的函数setup下,要是有权限的话直接连指定路径就可以了[DllImport("mpr.dll")] public static extern int WNetAddConnection2W( [MarshalAs(UnmanagedType.LPArray)] NETRESOURCEW[] lpNetResource, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, [MarshalAs(UnmanagedType.LPWStr)] string UserName, int dwFlags); [DllImport("mpr.dll")] public static extern int WNetCancelConnection2( string lpName, int dwFlags, bool fForce); public static int SetupNetShare(string remotepath, string username, string password) { NETRESOURCEW [] n = new NETRESOURCEW[1]; n[0] = new NETRESOURCEW(); n[0].dwType = 1; int dwFlags = 1; n[0].lpLocalName = null; n[0].lpRemoteName = remotepath; n[0].lpProvider = null;
if (mLog.IsDebugEnabled) mLog.Debug(n[0].ToString());
int res = WNetAddConnection2W( n, password, username, dwFlags ); return res; } public static int CancleNetShare(string remotepath, int dwFlags, bool fForce) { int res = WNetCancelConnection2(remotepath, dwFlags, fForce); return res; }
public static extern int WNetAddConnection2W(
[MarshalAs(UnmanagedType.LPArray)] NETRESOURCEW[] lpNetResource,
[MarshalAs(UnmanagedType.LPWStr)] string lpPassword,
[MarshalAs(UnmanagedType.LPWStr)] string UserName,
int dwFlags); [DllImport("mpr.dll")]
public static extern int WNetCancelConnection2(
string lpName,
int dwFlags,
bool fForce); public static int SetupNetShare(string remotepath, string username, string password)
{
NETRESOURCEW [] n = new NETRESOURCEW[1];
n[0] = new NETRESOURCEW();
n[0].dwType = 1;
int dwFlags = 1;
n[0].lpLocalName = null;
n[0].lpRemoteName = remotepath;
n[0].lpProvider = null;
if (mLog.IsDebugEnabled)
mLog.Debug(n[0].ToString());
int res = WNetAddConnection2W( n, password, username, dwFlags );
return res;
}
public static int CancleNetShare(string remotepath, int dwFlags, bool fForce)
{
int res = WNetCancelConnection2(remotepath, dwFlags, fForce);
return res;
}