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);
}
博客介绍了在C#中实现网络共享连接与断开的两种方法。一是通过执行命令,会有cmd窗口一闪而过;二是调用WINDOW的API函数,代码中包含连接和断开共享的具体实现,还对异常进行了处理。
6428

被折叠的 条评论
为什么被折叠?



