C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實(shí)現(xiàn)
在某些場(chǎng)景下我們需要遠(yuǎn)程訪問共享硬盤空間,從而實(shí)現(xiàn)方便快捷的訪問遠(yuǎn)程文件。比如公司局域網(wǎng)內(nèi)有一臺(tái)電腦存放了大量的文件,其它電腦想要訪問該電腦的文件,就可以通過網(wǎng)絡(luò)硬盤方式實(shí)現(xiàn),跟訪問本地硬盤同樣的操作,很方便且快速。通過C#我們可以實(shí)現(xiàn)網(wǎng)絡(luò)硬盤的自動(dòng)化管理。
創(chuàng)建一個(gè)類WebNetHelper,在類中加入如下成員變量及成員函數(shù),
static public WebNetHelper wnh=null; private string remoteHost;//遠(yuǎn)程主機(jī)的共享磁盤,形式如\\1.1.1.1\cc private string destionDisk;//要訪問的磁盤盤符 private string remoteUserName;//登錄遠(yuǎn)程主機(jī)的用戶名 private string passWord;//登錄遠(yuǎn)程主機(jī)的密碼
訪問網(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ù)為測(cè)試使用的過程。\\1.1.1.1\cc為網(wǎng)絡(luò)硬盤地址,K為要映射的盤符,"Noner"為遠(yuǎn)程主機(jī)的登錄名,"uiosdsau"為遠(yuǎn)程主機(jī)的密碼。Test函數(shù)為讀取網(wǎng)絡(luò)硬盤下的ImbaMallLog.txt文件內(nèi)容的第一行。
/// <summary>
/// 測(cè)試函數(shù),測(cè)試使用該類
/// </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ò)硬盤的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# 訪問網(wǎng)絡(luò)硬盤內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(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
Js中的substring,substr與C#中的Substring比較
本篇文章主要是對(duì)Js中的substring,substr與C#中的Substring進(jìn)行了比較。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01

