1. 程式人生 > >C# 讀寫網路上的芳鄰中的共享檔案

C# 讀寫網路上的芳鄰中的共享檔案

讀寫網路上的芳鄰共享的資料夾,和操作本地資料夾類似,只要有許可權讀取寫入即可。

分為以下2步:

1.打通共享資料夾許可權

2.操作檔案

 

 

打通共享資料夾許可權

 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 static bool ConnectState(string path, string userName, string passWord) 9 { 10 bool Flag = false; 11 Process proc = new Process();
12 try 13 { 14 proc.StartInfo.FileName = "cmd.exe"; 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 string dosLine = @"net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; 22 proc.StandardInput.WriteLine(dosLine); 23 proc.StandardInput.WriteLine("exit"); 24 while (!proc.HasExited) 25 { 26 proc.WaitForExit(1000); 27 } 28 string errormsg = proc.StandardError.ReadToEnd(); 29 proc.StandardError.Close(); 30 if (string.IsNullOrEmpty(errormsg)) 31 { 32 Flag = true; 33 } 34 else 35 { 36 throw new Exception(errormsg); 37 } 38 } 39 catch (Exception ex) 40 { 41 throw ex; 42 } 43 finally 44 { 45 proc.Close(); 46 proc.Dispose(); 47 } 48 49 return Flag; 50 }
View Code

 

建立資料夾

 

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

 

 

 

 

上傳檔案

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