SoFunction
Updated on 2025-03-07

Implementation of C# creation and access to network hard disk

In some scenarios, we need to remotely access the shared hard disk space to achieve convenient and fast access to remote files. For example, there is a computer in the company's LAN that stores a large number of files. If other computers want to access the computer's files, they can achieve it through the network hard disk. The same operation as accessing the local hard disk is very convenient and fast. Through C#, we can realize the automated management of network hard disks.

Create a class WebNetHelper, add the following member variables and member functions to the class.

static public WebNetHelper wnh=null;
private string remoteHost;//The shared disk of the remote host, in the form of \\1.1.1.1\ccprivate string destionDisk;//Disk letter to accessprivate string remoteUserName;//Login the username of the remote hostprivate string passWord;//Login the password of the remote host

Access the network hard drive,

public bool Connect()
{
    try
    {
        string cmdString = (@"net use {1}: {0} {3} /user:{2} >NUL",,
        , ,);
        (cmdString);
        return true;
    }
    catch (Exception e)
    {
        throw e;
    }
}

Disconnect the network mapping,

public bool Disconnect()
{
    try
    {
        string cmdString=(@"net use {0}: /delete >NUL",);
        (cmdString);
        return true;
    }
    catch (Exception e)
    {
        throw e;
    }
}

Execute CMD commands,

private bool WriteStringToComman(string cmdString)
{
    bool Flag = true;
    Process proc = new Process();
     = "";
     = false;
     = true;
     = true;
     = true;
     = true;
    try
    {
        ();
        string command = cmdString;
        (command);
        command = "exit";
        (command);
        while ( == false)
        {
            (1000);
        }
        string errormsg = ();
        if (errormsg != "")
            Flag = false;
        ();
        return Flag;
    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        ();
        ();
    }
}

Then the test function is used as the process used for testing. \\1.1.1.1\cc is the network hard disk address, K is the drive letter to be mapped, "Noner" is the login name of the remote host, and "uiosdsau" is the password of the remote host. The Test function is the first line of the file contents on the network hard disk.

/// <summary>
/// Test function, test using this class/// </summary>
private void test()
{
    try
    {
        if (!(@"K:\"))
        {
             = new WebNetHelper(@"\\1.1.1.1\cc", "K", "Noner", "uiosdsau");
            ();
        }
        StreamReader sr = new StreamReader(@"K:\");
        string tt = ();
        //(tt);
        ();
        ();
        if ( != null)
        {
            ();
        }
    }
    catch (Exception e)
    {
        //();
    }
}

This is the end of this article about C# creation and the implementation of accessing network hard disks. For more related C# content to access network hard disks, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!