本文实例为大家分享了C#连接服务器共享文件夹的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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(); } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/chao88552828/article/details/54600012