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

编辑: admin 分类: c#语言 发布时间: 2022-03-12 来源:互联网

本文实例为大家分享了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(); 
    【原URL http://www.yidunidc.com/kt.html 转载请说明出处】  if (myProcess != null) 
      { 
        myProcess.Close(); 
      } 
    } 
  } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。