Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Step 4: Write the Code!

Ooh! Ooh!

This is the part you've probably been waiting to see. Unhappily, this is also the part that relies almost entirely on you. I can't walk you through it. Dig into any documentation that you can find. Be a good developer. Don't get lost. Your old code will provide some guidelines into roughly how much code you'll need to write, but keep in mind step 2, "Understand the intent." Don't lose sight of new language features that could make your life easier. I've added some very basic exceptions in Listing 2, but you can see where I'm headed; we'll give some more detailed feedback to the developer. I've also added some error codes.

Listing 2—Translating to C# and adding some basic exceptions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Diagnostics;

namespace NetConnectSnippet
    class DriveMappingSnippet
        internal struct NETRESOURCE
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            public string lpLocalName;
            public string lpRemoteName;
            public string lpComment;
            public string lpProvider;

        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2W", 
        public static extern int WNetAddConnection2(ref NETRESOURCE lpNetResource, 
		string lpPassword, string lpUsername, Int32 dwFlags);

        public const int RESOURCETYPE_ANY = 0x0;
        public const int CONNECT_INTERACTIVE = 0x00000008;
        public const int CONNECT_PROMPT = 0x00000010;
        public const int RESOURCE_CONNECTED = 0x01;
        public const int RESOURCE_GLOBALNET = 0x02;
        public const int RESOURCETYPE_DISK = 0x01;
        public const int RESOURCEDISPLAYTYPE_SHARE = 0x03;
        public const int RESOURCEUSAGE_CONNECTABLE = 0x01;
        public const int CONNECT_UPDATE_PROFILE = 0x01;

        public const int NO_ERROR = 0;
        public const int ERROR_ACCESS_DENIED = 5;
        public const int ERROR_ALREADY_ASSIGNED = 85;
        public const int ERROR_BAD_DEV_TYPE = 66;
        public const int ERROR_BAD_DEVICE = 1200;
        public const int ERROR_BAD_NET_NAME = 67;
        public const int ERROR_BAD_PROFILE = 1206;
        public const int ERROR_BAD_PROVIDER = 1204;
        public const int ERROR_BUSY = 170;
        public const int ERROR_CANCELLED = 1223;
        public const int ERROR_CANNOT_OPEN_PROFILE = 1205;
        public const int ERROR_DEVICE_ALREADY_REMEMBERED = 1202;
        public const int ERROR_EXTENDED_ERROR = 1208;
        public const int ERROR_INVALID_PASSWORD = 86;
        public const int ERROR_NO_NET_OR_BAD_PATH = 1203;

        // Maps a remote file system to a free local drive letter.
        public void MapDrive(string LocalDrive, string RemoteDrive, string Username, string Password)
            if (!Directory.Exists(RemoteDrive))
                throw new System.Exception("The directory to be mapped doesn't exist!");

            if (Directory.Exists(LocalDrive))
                throw new System.Exception("The drive being mapped to already exists!");

            NETRESOURCE NetR = new NETRESOURCE();

            NetR.dwScope = RESOURCE_GLOBALNET;
            NetR.dwType = RESOURCETYPE_DISK;
            NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
            NetR.lpLocalName = LocalDrive;
            NetR.lpRemoteName = RemoteDrive;
            NetR.lpComment = null;
            NetR.lpProvider = null;

            int result = WNetAddConnection2(ref NetR, Username, Password, CONNECT_UPDATE_PROFILE);

            if (result == NO_ERROR)
                Console.WriteLine("Drive {0} mapped to local drive {1}", RemoteDrive, LocalDrive);
                Console.WriteLine("Unable to map {0} to local drive {1}", RemoteDrive, LocalDrive);
                throw new Exception("Error code " + result.ToString() + " was returned.");

It's a start!

  • + Share This
  • 🔖 Save To Your Account