C calls the API function to pop up the mapping network drive dialog problem

  • 2020-06-12 10:17:52
  • OfStack

1. Introduction to basic knowledge

First of all, there is no network-driven mapping dialog box in the common dialog box of.net in C#, so the API function of windows is needed to realize the popup dialog box of network-driven mapping.

The main points of calling the API function in c# are: the technical points of calling Windows API in C#

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

a, numerical type directly with the corresponding can be. (DWORD - > int , WORD - > Int16)
b, API string pointer type - > The net string
c, API handle (dWord) - > The net IntPtr
d, API structure - > .Structure or class in net. Note that in this case, you first qualify the declaration structure or class with the StructLayout feature

Then, in Windows, the API function that brings up the mapped network drive dialog is WNetConnectionDialog(HWND hwnd, DWORD dwType) and returns -1 if the dialog is cancelled and NO_ERROR if successful.

Can reference MSDNhttp: / / msdn. microsoft. com/en - us library/aa385433 (v = VS. 85). aspx

2. Code implementation

[DllImport("mpr.dll", CharSet = CharSet.Ansi)]private static extern int WNetConnectionDialog(IntPtr HWND, int dwType);
private void button1_Click(object sender, EventArgs e)
WNetConnectionDialog(this.Handle, 1);//RESOURCETYPE_DISK The value of 1

3. The test
Click the button and it pops up to configure

Related articles: