Mounting Network Drives with C#.net

Mounting Network Drives with C#.net
Photo by Patrick Lindenberg / Unsplash

First off, big thanks to aejw for making my life a lot easier by writing an awesome class to make this possible.

Download the project files from here, extract cNetworkDrives0015.cs and add it to your project.

using aejw.Network;


private bool mount_drive(string localdrive, string sharelocation, string user = "blank", string pass = "blank")
        {

            NetworkDrive oNetDrive = new NetworkDrive();

            try
            {
                oNetDrive.LocalDrive = localdrive;
                oNetDrive.ShareName = sharelocation;
                if (user == "blank")
                {
                    oNetDrive.MapDrive();
                }
                else
                {
                    oNetDrive.MapDrive(user, pass);
                }
            }
            catch (Exception err)
            {
                //Optional MessageBox to show Error
                //MessageBox.Show(this, "Error: " + err.Message);
                return false;
            }

            return true;

        }

Example Usage:


if (mount_drive("h", @"\\nas007\shared"))
            {
                //Alert that drive was mapped
            }
            else
            {
                //Alert that drive mapping failed
            }