英文:
Mapping network drives
问题
以下是翻译好的内容:
在WPF / MAUI中是否有任何可能的方法来映射网络驱动器(我认为对于两者都将是相同的,但我需要它用于WPF)。
我有一些想法(但我在网上没有找到任何信息):
-
使用某些类/库来执行此操作。仅使用简单的方法来映射磁盘并在以后需要时处理它将解决许多问题。
-
使用Windows命令进行映射。我将编写cmd / PowerShell命令来连接网络磁盘,然后在WPF中使用某个类来启动此命令/将此命令写入文件,然后以后执行它。
是否有任何选项?对我来说,第1个选项最好,但我不知道是否可能。所以给我一些信息?有什么想法?
英文:
Is there any possible way to map network drives in WPF / MAUI (i think it will work the same for both but i need it for WPF)
I have some ideas (but I didn't find anything online):
-
Use some class / library to do that. Use just simple methods for mapping disk and working with it later when I will need it. It will solve lots of problems.
-
Use windows commands to map it. I will write cmd / powerShell commands to connect network disk and later use some class from WPF to launch this command / write this command to file and later execute it.
Are there any options. 1. option will be best form me, but I don't know if it is possible. So give me some sources? Any ideas?
答案1
得分: 0
此博客文章建议您可以使用 WNetAddConnection2
Win32 API 来以编程方式映射网络驱动器:
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);
public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
{
// 检查最后一个字符是否为\,因为这会导致映射驱动器时出错。
if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
{
sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
}
NETRESOURCE oNetworkResource = new NETRESOURCE();
oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
oNetworkResource.sLocalName = sDriveLetter + ":";
oNetworkResource.sRemoteName = sNetworkPath;
WNetAddConnection2(ref oNetworkResource, null, null, 0);
}
这段代码用于以编程方式映射网络驱动器。
英文:
This blog post suggests that you could use the WNetAddConnection2
Win32 API to map a network drive programmatically:
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);
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();
oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
oNetworkResource.sLocalName = sDriveLetter + ":";
oNetworkResource.sRemoteName = sNetworkPath;
WNetAddConnection2(ref oNetworkResource, null, null, 0);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论