转载CSDN文章
1.通过执行命令,会有cmd窗口一闪而过:
public void Get_Share(string remotepath, string localpath, string username, string password)
{
Process.Start("net"," use "+localpath+" "+remotepath+" "+password+" /user:"+username);
}
public void Break_Share(string localpath)
{
Process.Start("net"," use "+localpath+" /delete");
}
2.通过调用WINDOW的API函数:
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A(NETRESOURCE [] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A(string sharename,int dwFlags,int fForce);
public int GetShare(string remotepath,string localpath,string username,string password)
{
try
{
NETRESOURCE [] share_driver = new NETRESOURCE[1];
share_driver[0].dwType = 1;
share_driver[0].lpLocalName = localpath;
share_driver[0].lpRemoteName = remotepath;
BreakShare(localpath);
int ret = WNetAddConnection2A(share_driver, password, username, 1);
return ret;
}
catch(Exception error)
{
throw new Exception(error.Message);
}
}
public void BreakShare(string localpath)
{
int ret= WNetCancelConnection2A(localpath, 1, 1);
}