SoFunction
Updated on 2025-03-08

C# Calling API function pops up the mapping network drive dialog box

1. Introduction to basic knowledge

First of all, there is no mapping network driver mapping dialog box in the common dialog box of .net in C#, so it is necessary to use the Windows API function to implement the pop-up mapping network driver dialog box.

The key points of calling API functions in c# can be used as reference:Technical points for calling Windows API in C#

It is worth noting that the declaration of parameter types in the .net environment is different:

a. Just use the corresponding numerical type directly. (DWORD -> int , WORD -> Int16)
b. String pointer type in API -> string in .net
c. Handle in API (dWord) -> IntPtr in .net
d. Structure in API  -> Structure or class in .net. Note that in this case, you must first use the StructLayout feature to define the declaration structure or class.

Then, in Windows, the API function that calls out the mapping network drive dialog box is WNetConnectionDialog(HWND hwnd, DWORD dwType). The return value of the function is if the dialog box is cancelled, and returns -1, and if successful, it returns NO_ERROR.

You can refer to MSDNhttp:///en-us/library/aa385433(v=VS.85).aspx

2. Code implementation

Copy the codeThe code is as follows:

[DllImport("", CharSet = )]private static extern int WNetConnectionDialog(IntPtr HWND, int dwType);
private void button1_Click(object sender, EventArgs e)
{
WNetConnectionDialog(, 1);//The value of RESOURCETYPE_DISK is 1
}

3. Test
Click the button and pop up, and configure it