C#創(chuàng)建及訪問(wèn)網(wǎng)絡(luò)硬盤(pán)的實(shí)現(xiàn)
在某些場(chǎng)景下我們需要遠(yuǎn)程訪問(wèn)共享硬盤(pán)空間,從而實(shí)現(xiàn)方便快捷的訪問(wèn)遠(yuǎn)程文件。比如公司局域網(wǎng)內(nèi)有一臺(tái)電腦存放了大量的文件,其它電腦想要訪問(wèn)該電腦的文件,就可以通過(guò)網(wǎng)絡(luò)硬盤(pán)方式實(shí)現(xiàn),跟訪問(wèn)本地硬盤(pán)同樣的操作,很方便且快速。通過(guò)C#我們可以實(shí)現(xiàn)網(wǎng)絡(luò)硬盤(pán)的自動(dòng)化管理。
創(chuàng)建一個(gè)類(lèi)WebNetHelper,在類(lèi)中加入如下成員變量及成員函數(shù),
static public WebNetHelper wnh=null; private string remoteHost;//遠(yuǎn)程主機(jī)的共享磁盤(pán),形式如\\1.1.1.1\cc private string destionDisk;//要訪問(wèn)的磁盤(pán)盤(pán)符 private string remoteUserName;//登錄遠(yuǎn)程主機(jī)的用戶名 private string passWord;//登錄遠(yuǎn)程主機(jī)的密碼
訪問(wèn)網(wǎng)絡(luò)硬盤(pán),
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; } }
斷開(kāi)網(wǎng)絡(luò)映射,
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; } }
執(zhí)行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函數(shù)為測(cè)試使用的過(guò)程。\\1.1.1.1\cc為網(wǎng)絡(luò)硬盤(pán)地址,K為要映射的盤(pán)符,"Noner"為遠(yuǎn)程主機(jī)的登錄名,"uiosdsau"為遠(yuǎn)程主機(jī)的密碼。Test函數(shù)為讀取網(wǎng)絡(luò)硬盤(pán)下的ImbaMallLog.txt文件內(nèi)容的第一行。
/// <summary> /// 測(cè)試函數(shù),測(cè)試使用該類(lèi) /// </summary> 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); } }
到此這篇關(guān)于C#創(chuàng)建及訪問(wèn)網(wǎng)絡(luò)硬盤(pán)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 訪問(wèn)網(wǎng)絡(luò)硬盤(pán)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C# 獲取硬盤(pán)號(hào),CPU信息,加密解密技術(shù)的步驟
- C#檢測(cè)移動(dòng)硬盤(pán)并獲取移動(dòng)硬盤(pán)盤(pán)符的方法
- C#獲取硬盤(pán)序列號(hào)的問(wèn)題小結(jié)
- C#實(shí)現(xiàn)讀取指定盤(pán)符硬盤(pán)序列號(hào)的方法
- C#獲取機(jī)器碼的方法詳解(機(jī)器名,CPU編號(hào),硬盤(pán)編號(hào),網(wǎng)卡mac等)
- C#獲取硬盤(pán)編號(hào)的方法
- C#利用win32 Api 修改本地系統(tǒng)時(shí)間、獲取硬盤(pán)序列號(hào)
- 用C#獲取硬盤(pán)序列號(hào),CPU序列號(hào),網(wǎng)卡MAC地址的源碼
相關(guān)文章
基于C# winform實(shí)現(xiàn)圖片上傳功能的方法
這篇文章主要介紹了基于C# winform實(shí)現(xiàn)圖片上傳功能的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07解析Silverlight調(diào)用WCF/Rest異常的解決方法
本篇文章對(duì)Silverlight調(diào)用WCF/Rest異常的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05利用WPF實(shí)現(xiàn)Windows屏保的制作
屏保程序的本質(zhì)上就是一個(gè)Win32?窗口應(yīng)用程序。本文將利用WPF實(shí)現(xiàn)Windows屏保的制作,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)WPF有一定幫助,感興趣的可以了解一下2022-07-07解決Unity urp級(jí)聯(lián)陰影接縫問(wèn)題
通過(guò)從unity內(nèi)部函數(shù)中抽幾個(gè)出來(lái)改造,強(qiáng)制取某個(gè)裁切球的級(jí)聯(lián)陰影映射,通過(guò)案例給大家詳細(xì)介紹,文中給出了完整的urp shader代碼,對(duì)Unity級(jí)聯(lián)陰影知識(shí)感興趣的朋友一起看看吧2021-06-06Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01