C# 讀寫網上鄰居中的共用文件

来源:https://www.cnblogs.com/yellowgiutou/archive/2018/11/20/9989692.html
-Advertisement-
Play Games

讀寫網上鄰居共用的文件夾,和操作本地文件夾類似,只要有許可權讀取寫入即可。 分為以下2步: 1.打通共用文件夾許可權 2.操作文件 打通共用文件夾許可權 1 /// <summary> 2 /// 連接共用文件 3 /// </summary> 4 /// <param name="path">共用文件地 ...


讀寫網上鄰居共用的文件夾,和操作本地文件夾類似,只要有許可權讀取寫入即可。

分為以下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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ASP.NET -- WebForm: Session的使用 ...
  • 有道翻譯有官方介面,也有非官方介面。 有道官方介面 官方介面:https://openapi.youdao.com/openapi 可查詢英語單詞註釋、單詞發音、網路釋義(不一定存在),可獲取線上發音地址。 具體使用,可參考 C# 有道API翻譯 查詢單詞詳細信息 有道非官方介面 何謂非官方介面,即 ...
  • 從周一到周日的順序,獲取排序數值: int i = DateTime.Now.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6; 獲取某日起,星期一日期: public static DateTime GetMondayDate(DateTime so ...
  • Stream s = Request.InputStream; int count = 0; byte[] buffer = new byte[1024]; StringBuilder reqXml = new StringBuilder(); while ((count = s.Read(buff ...
  • public SortedDictionary<string, string> GetRequestPost() { int i = 0; SortedDictionary<string, string> sArray = new SortedDictionary<string, string>() ...
  • 針對關係型資料庫,實體之間的關係最常見的就是通過外鍵關聯的一對一、一對多和多對多的關係,新的EF Core通過註釋和Fluent API 能夠做到接近於資料庫通過DML創建模型的效果了。實際上,通過DML 最大的優勢在於,能夠定義所謂的Independent/Dependent Entity具體的那 ...
  • 和這種蛋糕一樣,Docker的容器和鏡像也是使用類似的分層文件系統構建而成的。 這樣做的好處就是可以節省硬碟空間,也利於復用等等。因為Docker基於鏡像創建容器的時候,其鏡像是共用的;而且鏡像裡面的層如果已存在,也無需再下載。 下麵拉取一個mongodb的鏡像,拉取的過程中可以看到: 圖中紅框範圍 ...
  • 概述 泛型類和泛型方法兼具可重用性、類型安全性和效率,這是非泛型類和非泛型方法無法實現的 泛型通常與集合以及作用於集合的方法一起使用 泛型所屬命名空間:System.Collections.Generic 可以創建自定義泛型介面、泛型類、泛型方法、泛型事件和泛型委托,以提供自己的通用解決方案,設計類 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...