dnf卢克副本C#向共享⽂件夹上传⽂件研究了好久,才做成,以此做个⽇志。
⼀共两种⽅法
第⼀种:使⽤dos命令
1 /// <summary>
2 /// 连接共享⽂件
3 /// </summary>
4 /// <param name="path">共享⽂件地址</param>
5 /// <param name="userName">⽤户名</param>
6 /// <param name="passWord">密码</param>
7 /// <returns>true:连接成功 false:连接失败</returns>
8 public bool ConnectState(string path, string userName, string passWord, int islog)
9 {
10 bool Flag = false;
11 Process proc = new Process();
12 try
13 {
14 proc.StartInfo.FileName = "";
15 proc.StartInfo.UseShellExecute = false;
16 proc.StartInfo.RedirectStandardInput = true;
17 proc.StartInfo.RedirectStandardOutput = true;
18 proc.StartInfo.RedirectStandardError = true;
19 proc.StartInfo.CreateNoWindow = true;
20 proc.Start();
21 //登录验证
22 string dosLine = @"net use \\192.168.1.205\ipc$ 12345/user:ftpuser";
23 proc.StandardInput.WriteLine(dosLine);
24 proc.StandardInput.WriteLine("exit");
25 while (!proc.HasExited)
26 {
27 proc.WaitForExit(1000);
28 }
29 string errormsg = proc.StandardError.ReadToEnd();
30 proc.StandardError.Close();
31 if (string.IsNullOrEmpty(errormsg))
32 {
33 Flag = true;
34 }
35 else
36 {
37 if (islog > 0)
38 com.log("ConnectState", "连接共享⽂件信息:" + errormsg);
39 throw new Exception(errormsg);
40 }
41 }
42 catch (Exception ex)
43 {
44 if (islog > 0)
45 com.log("ConnectState", "连接共享⽂件出错:" + ex.Message);
46 //throw ex;
47 Flag = false;
48 }
49 finally
50 {
51 proc.Close();
52 proc.Dispose();
53 }
54
55 return Flag;
56 }
57
58 /// <summary>
59 /// 上传⽂件到共享⽂件夹
60 /// </summary>
61 /// <param name="sourceFile">本地⽂件</param>
62 /// <param name="remoteFile">远程⽂件</param>
63 public bool UpLoadFile(string sourceFile, string remoteFile, int islog)
64 {
65 //判断⽂件夹是否存在 ->不存在则创建
66 var targetFolder = Path.GetDirectoryName(remoteFile);
67 DirectoryInfo theFolder = new DirectoryInfo(targetFolder);
68 if (theFolder.Exists == false)
69 {
70 theFolder.Create();
71 }
72
73 var flag = true;
74
75 try
76 {
77 WebClient myWebClient = new WebClient();
78 NetworkCredential cread = new NetworkCredential();
79 myWebClient.Credentials = cread;
80
81 using (FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
82 {
83 using (BinaryReader r = new BinaryReader(fs))
84 {
85 byte[] postArray = r.ReadBytes((int)fs.Length);
86 using (Stream postStream = myWebClient.OpenWrite(remoteFile))
87 {
88 if (postStream.CanWrite == false)
89 {
90 //LogUtil.Error($"{remoteFile} ⽂件不允许写⼊~");
91 if (islog > 0)
92 com.log("UpLoadFile", remoteFile + " ⽂件不允许写⼊~");
93 flag= false;
94 }
95
96 postStream.Write(postArray, 0, postArray.Length);
97 }
98 }
99 }
100
101 return flag;
102 }
103 catch (Exception ex)
104 {
105 // string errMsg = $"{remoteFile} ex:{ex.ToString()}";
106 //LogUtil.Error(errMsg);
107 //Console.WriteLine(ex.Message);
108 if (islog > 0)
109 com.log("UpLoadFile", "上传⽂件到共享⽂件夹:" + ex.Message);
110 return false;
111 }
112 }
使⽤这个⽅法之前,先打开cmd窗⼝,⽤dos命令运⾏是否正常
命令:打开连接:net use \\IP地址\ipc$ 密码/user:⽤户名 注意:只有三个空格
删除连接:net use \\IP地址\ipc$ 密码/user:⽤户名\del
net use错误解决⽅案:
1.错误号5,拒绝访问:很可能你使⽤的⽤户不是管理员权限的,先提升权限;
2.错误号51,Windows⽆法到⽹络路径:⽹络有问题;
3.错误号53,不到⽹络路径:ip地址错误;⽬标未开机;⽬标lanmanserver服务未启动;⽬标有防⽕墙(端⼝过滤);
4.错误号67,不到⽹络名:你的lanmanworkstation服务未启动或者⽬标删除了ipc$;办理营业执照>立冬祝福语大全简短赞美
5.错误号1219,提供的凭据与已存在的凭据集冲突:你已经和对⽅建⽴了⼀个ipc$,请删除再连;
6.错误号1326,未知的⽤户名或错误密码:原因很明显了;
WinXP:控制⾯板-〉⽂件夹选项-〉察看-〉简单的⽂件共享去掉选取。然后再尝试连接。果真是这个“简单⽂件共享”搞的⿁,把它取消就可以了。简单⽂件共享会把⽹络连接权限都归为 guest连接,是⽆法访问C$等管理共享的
win2003:运⾏->输⼊secpol.msc,打开本地安全设置->本地策略->安全选项->选择"⽹络安全:LAN管理器⾝份验证级别"的属性修改为“发送LM和NTLM响应”即可~
WIN7:是安全设置有⼀项要设置!本地安全策略-本地策略-安全选项-⽹络安全:LAN管理器⾝份验证级别,默认是“没有定义”,更改为“发送LM和NTLM响应!
重启组策略:cmd:gpupdate
奥特曼技能
或者Guest⽤户取消禁⽤
7.错误号1792,试图登录,但是⽹络登录服务没有启动:⽬标NetLogon服务未启动;
8.错误号2242,此⽤户的密码已经过期:⽬标有帐号策略,强制定期要求更改密码.
第⼆种⽅法:
1 /// <summary>
2 /// 上传⽂件:要设置共享⽂件夹是否有创建的权限,否则⽆法上传⽂件
3 /// </summary>
4 /// <param name="fileNamePath">本地⽂件路径</param>
5 /// <param name="urlPath">共享⽂件夹地址</param>
6 /// <param name="User"></param>
7 /// <param name="Pwd"></param>
8 /// <param name="islog"></param>
9 /// <returns></returns>
10 public bool UpLoadFile2(string fileNamePath, string urlPath, string User, string Pwd, int islog)
11 {
12 var flag = false;
13 string newFileName = fileNamePath.Substring(fileNamePath.LastIndexOf(@"\") + 1);//取⽂件名称
14 //MessageBox.Show(newFileName);
15 if (urlPath.EndsWith(@"\") == false) urlPath = urlPath + @"\";
16
17 urlPath = urlPath + newFileName;
18
19 WebClient myWebClient = new WebClient();
20 NetworkCredential cread = new NetworkCredential(User, Pwd, "Domain");
21 myWebClient.Credentials = cread;
22 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
23 BinaryReader r = new BinaryReader(fs);
24
25 Stream postStream =null;
26 try
27 {
28 byte[] postArray = r.ReadBytes((int)fs.Length);
29 postStream = myWebClient.OpenWrite(urlPath);
30 // postStream.m
31 if (postStream.CanWrite)
32 {
33 postStream.Write(postArray, 0, postArray.Length);
塞尔维亚瑞士比分预测34 //MessageBox.Show("⽂件上传成功!", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Information);
35 flag= true;
36 }
黄山风景区37 else
38 {
39 //MessageBox.Show("⽂件上传错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
40 flag= false;
41 }
42
43 postStream.Close();
44 return flag;
45 }
46 catch (Exception ex)
47 {
48 //MessageBox.Show(ex.Message, "错误");
49 if (islog > 0)
50 com.log("UpLoadFile", "上传⽂件到共享⽂件夹:" + ex.Message);
51 if (postStream != null)
52 postStream.Close();
53 return false;
54 }
55
56 }
⼀开始直接打开⽂档窗⼝,输⼊共享⽂件地址访问是没有问题的,代码连接却总是报错,估计跟Guest被禁⽌的原因
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论