BlackWaspTM
Network and Internet
.NET 1.1+

Mapping a Drive Letter Programmatically (2)

Some legacy applications do not permit the use of UNC paths when accessing network folders, instead requiring that a drive letter be mapped. When interacting with such software it may be necessary to map a drive letter and later remove mappings using C#.

Defining the NETRESOURCE Structure

The first parameter of WNetAddConnection2, and the second of WNetAddConnection3, accepts a NETRESOURCE structure. This structure contains information relating to the remote resource that you wish to connect to and the drive letter that you want to assign. Before you can use the mapping commands you must declare this structure, ensuring that it exactly matches that expected by the API. To create the structure, add the following:

[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
    public uint dwScope;
    public uint dwType;
    public uint dwDisplayType;
    public uint dwUsage;
    public string lpLocalName;
    public string lpRemoteName;
    public string lpComment;
    public string lpProvider;
}

Although the structure defines eight fields, only four of these are required when mapping drive letters. The first, dwType, is used to specify that we want to map to a network folder. This value is always set to a constant value named RESOURCETYPE_DISK. This constant must be defined within the code as follows:

const uint RESOURCETYPE_DISK = 1;

The lpLocalName field holds the drive letter that will be assigned to the mapped path, which is specified in lpRemoteName. The fourth value that must be provided is lpProvider. This string value determines the network provider that will be used to make the connection. However, normally this is set to null to specify that the operating system should automatically determine the provider to use.

Connection Flags

When the connection functions are called, a series of flags can be passed to the command to affect its behaviour. There are six flags, each defined in the table below:

Flag NameValueDescription
CONNECT_UPDATE_PROFILE0x1If this flag is set, the operating system is instructed to remember the mapping of the drive letter in the user's profile. This means that if the user logs off, when they log on again at a later date, an attempt to restore the connection will be made.
CONNECT_INTERACTIVE0x8When this flag is set, the operating system is permitted to ask the user for authentication information before attempting to map the drive letter.
CONNECT_PROMPT0x10When set, this flag indicates that any default user name and password credentials will not be used without first giving the user the opportunity to override them. This flag is ignored if CONNECT_INTERACTIVE is not also specified.
CONNECT_REDIRECT0x80This flag forces the redirection of a local device when making the connection. For the functionality described in this article the flag has no effect. It is included here for completeness.
CONNECT_COMMANDLINE0x800This flag indicates that if the operating system needs to ask for a user name and password, it should do so using the command line rather than by using dialog boxes. This flag is ignored if CONNECT_INTERACTIVE is not also specified. It is not available to Windows 2000 or earlier versions of the operating system.
CONNECT_CMD_SAVECRED0x1000If set, this flag specifies that any credentials entered by the user will be saved. If it is not possible to save the credentials or the CONNECT_INTERACTIVE is not also specified then the flag is ignored.

We can create these values in the program by defining the following constants:

const uint CONNECT_UPDATE_PROFILE = 0x1;
const uint CONNECT_INTERACTIVE = 0x8;
const uint CONNECT_PROMPT = 0x10;
const uint CONNECT_REDIRECT = 0x80;
const uint CONNECT_COMMANDLINE = 0x800;
const uint CONNECT_CMD_SAVECRED = 0x1000;
21 September 2008