溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#如何創(chuàng)建及訪問網(wǎng)絡硬盤

發(fā)布時間:2022-03-22 11:02:31 來源:億速云 閱讀:303 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)C#如何創(chuàng)建及訪問網(wǎng)絡硬盤,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

在某些場景下我們需要遠程訪問共享硬盤空間,從而實現(xiàn)方便快捷的訪問遠程文件。比如公司局域網(wǎng)內(nèi)有一臺電腦存放了大量的文件,其它電腦想要訪問該電腦的文件,就可以通過網(wǎng)絡硬盤方式實現(xiàn),跟訪問本地硬盤同樣的操作,很方便且快速。通過C#我們可以實現(xiàn)網(wǎng)絡硬盤的自動化管理。

創(chuàng)建一個類WebNetHelper,在類中加入如下成員變量及成員函數(shù),

static public WebNetHelper wnh=null;
private string remoteHost;//遠程主機的共享磁盤,形式如\\1.1.1.1\cc
private string destionDisk;//要訪問的磁盤盤符
private string remoteUserName;//登錄遠程主機的用戶名
private string passWord;//登錄遠程主機的密碼

訪問網(wǎng)絡硬盤,

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)絡映射,

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)絡硬盤地址,K為要映射的盤符,"Noner"為遠程主機的登錄名,"uiosdsau"為遠程主機的密碼。Test函數(shù)為讀取網(wǎng)絡硬盤下的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)絡硬盤”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI