0
我跟着approache到网络驱动器在以下链接程序映射: enter link description here映射网络驱动器C#编程在Windows 10
驱动器似乎是正确连接,因为我可以查询C#中的目录和文件。但我没有看到我的电脑上的驱动器。 我的目标是以编程方式为我的用户映射某些驱动器。 我不想使用批处理/ cmd ... 是否有可能与Windows 10的问题,或者是这种代码只是编程方法的好处。
亲切的问候
使用代码:
Utility.NetworkDrive.MapNetworkDrive("R", @"\\unc\path");
var dirs = Directory.GetDirectories("R:"); // got many nice directories...
Utility.NetworkDrive.DisconnectNetworkDrive("R", true);
全码:
namespace Utility
{
public class NetworkDrive
{
private enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
}
private enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
}
private enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010
}
private enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
}
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE
{
public ResourceScope oResourceScope;
public ResourceType oResourceType;
public ResourceDisplayType oDisplayType;
public ResourceUsage oResourceUsage;
public string sLocalName;
public string sRemoteName;
public string sComments;
public string sProvider;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2
(ref NETRESOURCE oNetworkResource, string sPassword,
string sUserName, int iFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2
(string sLocalName, uint iFlags, int iForce);
public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
{
//Checks if the last character is \ as this causes error on mapping a drive.
if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
{
sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
}
NETRESOURCE oNetworkResource = new NETRESOURCE()
{
oResourceType = ResourceType.RESOURCETYPE_DISK,
sLocalName = sDriveLetter + ":",
sRemoteName = sNetworkPath
};
//If Drive is already mapped disconnect the current
//mapping before adding the new mapping
if (IsDriveMapped(sDriveLetter))
{
DisconnectNetworkDrive(sDriveLetter, true);
}
WNetAddConnection2(ref oNetworkResource, null, null, 0);
}
public static int DisconnectNetworkDrive(string sDriveLetter, bool bForceDisconnect)
{
if (bForceDisconnect)
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 1);
}
else
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 0);
}
}
public static bool IsDriveMapped(string sDriveLetter)
{
string[] DriveList = Environment.GetLogicalDrives();
for (int i = 0; i < DriveList.Length; i++)
{
if (sDriveLetter + ":\\" == DriveList[i].ToString())
{
return true;
}
}
return false;
}
}
}
你是从哪个进程运行的? – Quietust
我试着在VS调试模式下启动编译好的exe文件。全部在我登录的用户帐户下。 – Mario
你有没有尝试过这些方面的东西? https://channel9.msdn.com/Forums/TechOff/242590-The-best-way-to-effect-quotnet-usequot-from-C – Forty3