经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 读写网上邻居中的共享文件
来源:cnblogs  作者:yellowgiutou  时间:2018/11/20 17:51:37  对本文有异议

读写网上邻居共享的文件夹,和操作本地文件夹类似,只要有权限读取写入即可。

分为以下2步:

1.打通共享文件夹权限

2.操作文件

 

 

打通共享文件夹权限

  1. 1 /// <summary>
  2. 2 /// 连接共享文件
  3. 3 /// </summary>
  4. 4 /// <param name="path">共享文件地址</param>
  5. 5 /// <param name="userName">用户名</param>
  6. 6 /// <param name="passWord">密码</param>
  7. 7 /// <returns>true:连接成功 false:连接失败</returns>
  8. 8 public static bool ConnectState(string path, string userName, string passWord)
  9. 9 {
  10. 10 bool Flag = false;
  11. 11 Process proc = new Process();
  12. 12 try
  13. 13 {
  14. 14 proc.StartInfo.FileName = "cmd.exe";
  15. 15 proc.StartInfo.UseShellExecute = false;
  16. 16 proc.StartInfo.RedirectStandardInput = true;
  17. 17 proc.StartInfo.RedirectStandardOutput = true;
  18. 18 proc.StartInfo.RedirectStandardError = true;
  19. 19 proc.StartInfo.CreateNoWindow = true;
  20. 20 proc.Start();
  21. 21 string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
  22. 22 proc.StandardInput.WriteLine(dosLine);
  23. 23 proc.StandardInput.WriteLine("exit");
  24. 24 while (!proc.HasExited)
  25. 25 {
  26. 26 proc.WaitForExit(1000);
  27. 27 }
  28. 28 string errormsg = proc.StandardError.ReadToEnd();
  29. 29 proc.StandardError.Close();
  30. 30 if (string.IsNullOrEmpty(errormsg))
  31. 31 {
  32. 32 Flag = true;
  33. 33 }
  34. 34 else
  35. 35 {
  36. 36 throw new Exception(errormsg);
  37. 37 }
  38. 38 }
  39. 39 catch (Exception ex)
  40. 40 {
  41. 41 throw ex;
  42. 42 }
  43. 43 finally
  44. 44 {
  45. 45 proc.Close();
  46. 46 proc.Dispose();
  47. 47 }
  48. 48
  49. 49 return Flag;
  50. 50 }
View Code

 

创建文件夹

 

  1. 1 DirectoryInfo dirInfo = new DirectoryInfo("\\WIN-R3377JMR1LG\ShareFolder");
  2. 2 if (dirInfo.Exists == false)
  3. 3 {
  4. 4 dirInfo.Create();
  5. 5 }
View Code

 

 

 

 

上传文件

  1. 1 /// <summary>
  2. 2 /// 上传文件到共享文件夹
  3. 3 /// </summary>
  4. 4 /// <param name="sourceFile">本地文件</param>
  5. 5 /// <param name="remoteFile">远程文件</param>
  6. 6 public static void UpLoadFile(string sourceFile, string remoteFile)
  7. 7 {
  8. 8 //判断文件夹是否存在 ->不存在则创建
  9. 9 var targetFolder = Path.GetDirectoryName(remoteFile);
  10. 10 DirectoryInfo theFolder = new DirectoryInfo(targetFolder);
  11. 11 if (theFolder.Exists == false)
  12. 12 {
  13. 13 theFolder.Create();
  14. 14 }
  15. 15
  16. 16 try
  17. 17 {
  18. 18 WebClient myWebClient = new WebClient();
  19. 19 NetworkCredential cread = new NetworkCredential();
  20. 20 myWebClient.Credentials = cread;
  21. 21
  22. 22 using (FileStream fs = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
  23. 23 {
  24. 24 using (BinaryReader r = new BinaryReader(fs))
  25. 25 {
  26. 26 byte[] postArray = r.ReadBytes((int)fs.Length);
  27. 27 using (Stream postStream = myWebClient.OpenWrite(remoteFile))
  28. 28 {
  29. 29 if (postStream.CanWrite == false)
  30. 30 {
  31. 31 LogUtil.Error($"{remoteFile} 文件不允许写入~");
  32. 32 return;
  33. 33 }
  34. 34
  35. 35 postStream.Write(postArray, 0, postArray.Length);
  36. 36 }
  37. 37 }
  38. 38 }
  39. 39 }
  40. 40 catch (Exception ex)
  41. 41 {
  42. 42 string errMsg = $"{remoteFile} ex:{ex.ToString()}";
  43. 43 LogUtil.Error(errMsg);
  44. 44 Console.WriteLine(errMsg);
  45. 45 }
  46. 46 }
View Code

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号