如何使用C#映射驱动器?驱动器、如何使用

2023-09-03 03:31:41 作者:闹腾i

使用C#我如何映射网络驱动器。我不想使用 NET USE 或任何第三方的API。

How can I map a network drive using C#. I don't want to use net use or any third party API.

听说了C#code UNC路径,但不太清楚如何去做。

Heard about UNC paths in C# code but not quite sure how to go about it.

推荐答案

使用的 WnetAddConnection 在本机可用的功能 MPR.DLL

您将不得不写的P / Invoke签名和结构,以调用通过对非托管函数。你可以找到P上的资源/调用在 pinvoke.net 。

You will have to write the P/Invoke signatures and structures to call through to the unmanaged function. You can find resources on P/Invoke on pinvoke.net.

这是的签名 WNetAddConnection2 上pinvoke.net :

This is the signature for WNetAddConnection2 on pinvoke.net:

[DllImport("mpr.dll")]    
public static extern int WNetAddConnection2(
   ref NETRESOURCE netResource,
   string password, 
   string username, 
   int flags);