C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實現(xiàn)
在某些場景下我們需要遠(yuǎn)程訪問共享硬盤空間,從而實現(xiàn)方便快捷的訪問遠(yuǎn)程文件。比如公司局域網(wǎng)內(nèi)有一臺電腦存放了大量的文件,其它電腦想要訪問該電腦的文件,就可以通過網(wǎng)絡(luò)硬盤方式實現(xiàn),跟訪問本地硬盤同樣的操作,很方便且快速。通過C#我們可以實現(xiàn)網(wǎng)絡(luò)硬盤的自動化管理。
創(chuàng)建一個類WebNetHelper,在類中加入如下成員變量及成員函數(shù),
static public WebNetHelper wnh=null; private string remoteHost;//遠(yuǎn)程主機的共享磁盤,形式如\\1.1.1.1\cc private string destionDisk;//要訪問的磁盤盤符 private string remoteUserName;//登錄遠(yuǎn)程主機的用戶名 private string passWord;//登錄遠(yuǎn)程主機的密碼
訪問網(wǎng)絡(luò)硬盤,
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;
}
}
斷開網(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ù)為測試使用的過程。\\1.1.1.1\cc為網(wǎng)絡(luò)硬盤地址,K為要映射的盤符,"Noner"為遠(yuǎn)程主機的登錄名,"uiosdsau"為遠(yuǎn)程主機的密碼。Test函數(shù)為讀取網(wǎng)絡(luò)硬盤下的ImbaMallLog.txt文件內(nèi)容的第一行。
/// <summary>
/// 測試函數(shù),測試使用該類
/// </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ǎng)絡(luò)硬盤的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# 訪問網(wǎng)絡(luò)硬盤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#實現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志
這篇文章主要介紹了c#實現(xiàn)服務(wù)器性能監(jiān)控并發(fā)送郵件保存日志的示例,代碼分為客戶端和服務(wù)端,客戶端可安裝為本地服務(wù)形式啟動2014-01-01
Winform圓形環(huán)繞的Loading動畫實現(xiàn)代碼
這篇文章主要介紹了Winform圓形環(huán)繞的Loading動畫實現(xiàn)代碼,有需要的朋友可以參考一下2014-01-01
動態(tài)webservice調(diào)用接口并讀取解析返回結(jié)果
webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個webservice暴露在外面可供使用的接口。今天我們來詳細(xì)討論下如何動態(tài)調(diào)用以及讀取解析返回結(jié)果2015-06-06
深入理解c# checked unchecked 關(guān)鍵字
本篇文章是對c#中的checked unchecked 關(guān)鍵字進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

