網站首頁 編程語言 正文
在某些場景下我們需要遠程訪問共享硬盤空間,從而實現方便快捷的訪問遠程文件。比如公司局域網內有一臺電腦存放了大量的文件,其它電腦想要訪問該電腦的文件,就可以通過網絡硬盤方式實現,跟訪問本地硬盤同樣的操作,很方便且快速。通過C#我們可以實現網絡硬盤的自動化管理。
創建一個類WebNetHelper,在類中加入如下成員變量及成員函數,
static public WebNetHelper wnh=null; private string remoteHost;//遠程主機的共享磁盤,形式如\\1.1.1.1\cc private string destionDisk;//要訪問的磁盤盤符 private string remoteUserName;//登錄遠程主機的用戶名 private string passWord;//登錄遠程主機的密碼
訪問網絡硬盤,
public bool Connect() { try { string cmdString = string.Format(@"net use {1}: {0} {3} /user:{2} >NUL",this.RemoteHost, this.DestionDisk, this.RemoteUserName,this.PassWord); this.WriteStringToComman(cmdString); return true; } catch (Exception e) { throw e; } }
斷開網絡映射,
public bool Disconnect() { try { string cmdString=string.Format(@"net use {0}: /delete >NUL",this.DestionDisk); this.WriteStringToComman(cmdString); return true; } catch (Exception e) { throw e; } }
執行CMD命令,
private bool WriteStringToComman(string cmdString) { bool Flag = true; Process proc = new Process(); proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; try { proc.Start(); string command = cmdString; proc.StandardInput.WriteLine(command); command = "exit"; proc.StandardInput.WriteLine(command); while (proc.HasExited == false) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); if (errormsg != "") Flag = false; proc.StandardError.Close(); return Flag; } catch (Exception e) { throw e; } finally { proc.Close(); proc.Dispose(); } }
然后test函數為測試使用的過程。\\1.1.1.1\cc為網絡硬盤地址,K為要映射的盤符,"Noner"為遠程主機的登錄名,"uiosdsau"為遠程主機的密碼。Test函數為讀取網絡硬盤下的ImbaMallLog.txt文件內容的第一行。
////// 測試函數,測試使用該類 /// private void test() { try { if (!Directory.Exists(@"K:\")) { WebNetHelper.wnh = new WebNetHelper(@"\\1.1.1.1\cc", "K", "Noner", "uiosdsau"); WebNetHelper.wnh.Connect(); } StreamReader sr = new StreamReader(@"K:\ImbaMallLog.txt"); string tt = sr.ReadLine(); //MessageBox.Show(tt); sr.Close(); sr.Dispose(); if (WebNetHelper.wnh != null) { WebNetHelper.wnh.Disconnect(); } } catch (Exception e) { //MessageBox.Show(e.Message); } }
原文鏈接:https://blog.csdn.net/huzhizhewudi/article/details/123614310
相關推薦
- 2022-09-05 UserWarning: indexing with dtype torch.uint8 is no
- 2022-10-27 C#利用Spire.Pdf包實現為PDF添加數字簽名_C#教程
- 2022-06-23 Android中的dumpsys命令詳解_Android
- 2023-02-12 Golang反射模塊reflect使用方式示例詳解_Golang
- 2022-07-25 C#爬蟲基礎之HttpClient獲取HTTP請求與響應_C#教程
- 2022-05-10 git reset中hard與soft區別
- 2022-10-09 ASP.NET?Core?5.0中的Host.CreateDefaultBuilder執行過程解析_
- 2022-04-18 python全面解析接口返回數據_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支