18 Temmuz 2010 Pazar

Mapping Network Drives using C#

You can use the Windows Networking Functions "WNetAddConnection2" and "WNetCancelConnection2" to map the drives. You can make a connection to a network resource by using the "WNetAddConnection2" function and can cancel an existing network connection by using the "WNetCancelConnection2" function.

Sample file can be downloaded from the following links:
NetworkDriveMapper.cs (Google)
NetworkDriveMapper.cs (Uploading)



using System;
using System.Runtime.InteropServices;

namespace Mapping_Network_Drive
{
    public class NetworkDriveMapper
    {
        private enum ResourceScope
        {
            RESOURCE_CONNECTED = 1,
            RESOURCE_GLOBALNET,
            RESOURCE_REMEMBERED,
            RESOURCE_RECENT,
            RESOURCE_CONTEXT
        }

        private enum ResourceType
        {
            RESOURCETYPE_ANY,
            RESOURCETYPE_DISK,
            RESOURCETYPE_PRINT,
            RESOURCETYPE_RESERVED
        }

        private enum ResourceUsage
        {
            RESOURCEUSAGE_CONNECTABLE = 1,
            RESOURCEUSAGE_CONTAINER = 2,
            RESOURCEUSAGE_NOLOCALDEVICE = 4,
            RESOURCEUSAGE_SIBLING = 8,
            RESOURCEUSAGE_ATTACHED = 10
        }

        private enum ResourceDisplayType
        {
            RESOURCEDISPLAYTYPE_GENERIC,
            RESOURCEDISPLAYTYPE_DOMAIN,
            RESOURCEDISPLAYTYPE_SERVER,
            RESOURCEDISPLAYTYPE_SHARE,
            RESOURCEDISPLAYTYPE_FILE,
            RESOURCEDISPLAYTYPE_GROUP,
            RESOURCEDISPLAYTYPE_NETWORK,
            RESOURCEDISPLAYTYPE_ROOT,
            RESOURCEDISPLAYTYPE_SHAREADMIN,
            RESOURCEDISPLAYTYPE_DIRECTORY,
            RESOURCEDISPLAYTYPE_TREE,
            RESOURCEDISPLAYTYPE_NDSCONTAINER
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct NetResource
        {
            public ResourceScope ResourceScope;
            public ResourceType ResourceType;
            public ResourceDisplayType DisplayType;
            public ResourceUsage ResourceUsage;
            public string LocalName;
            public string RemoteName;
            public string Comments;
            public string Provider;
        }

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(ref NetResource networkResource, string password,string userName, int iFlags);

        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string localName, uint iFlags, int iForce);

        public static void MapNetworkDrive(string driveLetter, string networkPath)
        {
            if (networkPath.EndsWith(@"\")) //When the last character is '\' , this causes error on mapping a drive.
            {
                networkPath = networkPath.Substring(0, networkPath.Length - 1);
            }

            NetResource networkResource = new NetResource();
            networkResource.ResourceType = ResourceType.RESOURCETYPE_DISK;
            networkResource.LocalName = driveLetter + ":";
            networkResource.RemoteName = networkPath;

            //If this drive is currently mapped, first disconnect the mapping before adding the new one
            if (IsDriveMapped(driveLetter))
            {
                DisconnectNetworkDrive(driveLetter, true);
            }

            WNetAddConnection2(ref networkResource, null, null, 0);
        }

        public static int DisconnectNetworkDrive(string driveLetter, bool forceDisconnect)
        {
            if (forceDisconnect)
            {
                return WNetCancelConnection2(driveLetter + ":", 0, 1);
            }
            else
            {
                return WNetCancelConnection2(driveLetter + ":", 0, 0);
            }
        }

        public static bool IsDriveMapped(string driveLetter)
        {
            string[] driveList = Environment.GetLogicalDrives();
            for (int i = 0; i < driveList.Length; i++)
            {
                if (driveLetter + ":\\" == driveList[i].ToString())
                {
                    return true;
                }
            }
            return false;
        }
    }
}

Hiç yorum yok: