Tuesday, March 20, 2012

Mounting Network Drives with C#.net

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.

Then: 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
            }





Source:
http://www.codeproject.com/Articles/6847/Map-Network-Drive-API

No comments:

Post a Comment

itninja.com