C#如何连接服务器共享文件夹
程序员文章站
2023-12-18 08:57:10
本文实例为大家分享了c#连接服务器共享文件夹的具体代码,供大家参考,具体内容如下
public class classconnwin
{
/// &l...
本文实例为大家分享了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(); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。