C#如何连接服务器共享文件夹

本文实例为大家分享了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#如何连接服务器共享文件夹 的全部内容, 来源链接: utcz.com/z/348226.html

回到顶部