本文实例为大家分享了C#连接服务器共享文件夹的具体代码,供大家参考,具体内容如下
public class ClassConnWin
{
/// <summary>
/// 连接共享文件夹
/// </summary>
/// <param name="path">共享路径</param>
/// <param name="user">用户名</param>
/// <param name="pass">密码</param>
/// <returns></returns>
public static void linkFile(string path, string user, string pass)
{
string cLinkUrl = @"Net Use " + path + " " + pass + " /user:" + user;
CallCmd(cLinkUrl);
}
/// <summary>
/// 关闭所有共享连接
/// </summary>
public static void KillAllLink()
{
string cKillCmd = @"Net Use /delete * /yes";
CallCmd(cKillCmd);
}
/// <summary>
/// 关闭指定连接
/// </summary>
/// <param name="path">共享路径</param>
public static void KillLink(string path)
{
string cKillCmd = @"Net Use " + path + " /delete /yes";
CallCmd(cKillCmd);
}
/// <summary>
/// 调用Cmd命令
/// </summary>
/// <param name="strCmd">命令行参数</param>
private static void CallCmd(string strCmd)
{
//调用cmd命令
Process myProcess = new Process();
try
{
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c " + strCmd;
myProcess.StartInfo.UseShellExecute = false; //关闭Shell的使用
myProcess.StartInfo.RedirectStandardInput = true; //重定向标准输入
myProcess.StartInfo.RedirectStandardOutput = true; //重定向标准输出
myProcess.StartInfo.RedirectStandardError = true; //重定向错误输出
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch { }
finally
{
myProcess.WaitForExit();
if (myProcess != null)
{
myProcess.Close();
}
}
}
}
您可能感兴趣的文章:C#如何访问共享文件夹或者磁盘解析使用C# lock同时访问共享数据C#读写共享文件夹的方法C#访问网络共享文件夹的方法