Mapping network drives (映射网络驱动器)

huangapple go评论100阅读模式
英文:

Mapping network drives

问题

以下是翻译好的内容:

在WPF / MAUI中是否有任何可能的方法来映射网络驱动器(我认为对于两者都将是相同的,但我需要它用于WPF)。

我有一些想法(但我在网上没有找到任何信息):

  1. 使用某些类/库来执行此操作。仅使用简单的方法来映射磁盘并在以后需要时处理它将解决许多问题。

  2. 使用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):

  1. 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.

  2. 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 来以编程方式映射网络驱动器:

  1. private enum ResourceScope
  2. {
  3. RESOURCE_CONNECTED = 1,
  4. RESOURCE_GLOBALNET,
  5. RESOURCE_REMEMBERED,
  6. RESOURCE_RECENT,
  7. RESOURCE_CONTEXT
  8. }
  9. private enum ResourceType
  10. {
  11. RESOURCETYPE_ANY,
  12. RESOURCETYPE_DISK,
  13. RESOURCETYPE_PRINT,
  14. RESOURCETYPE_RESERVED
  15. }
  16. private enum ResourceUsage
  17. {
  18. RESOURCEUSAGE_CONNECTABLE = 0x00000001,
  19. RESOURCEUSAGE_CONTAINER = 0x00000002,
  20. RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
  21. RESOURCEUSAGE_SIBLING = 0x00000008,
  22. RESOURCEUSAGE_ATTACHED = 0x00000010
  23. }
  24. private enum ResourceDisplayType
  25. {
  26. RESOURCEDISPLAYTYPE_GENERIC,
  27. RESOURCEDISPLAYTYPE_DOMAIN,
  28. RESOURCEDISPLAYTYPE_SERVER,
  29. RESOURCEDISPLAYTYPE_SHARE,
  30. RESOURCEDISPLAYTYPE_FILE,
  31. RESOURCEDISPLAYTYPE_GROUP,
  32. RESOURCEDISPLAYTYPE_NETWORK,
  33. RESOURCEDISPLAYTYPE_ROOT,
  34. RESOURCEDISPLAYTYPE_SHAREADMIN,
  35. RESOURCEDISPLAYTYPE_DIRECTORY,
  36. RESOURCEDISPLAYTYPE_TREE,
  37. RESOURCEDISPLAYTYPE_NDSCONTAINER
  38. }
  39. [StructLayout(LayoutKind.Sequential)]
  40. private struct NETRESOURCE
  41. {
  42. public ResourceScope oResourceScope;
  43. public ResourceType oResourceType;
  44. public ResourceDisplayType oDisplayType;
  45. public ResourceUsage oResourceUsage;
  46. public string sLocalName;
  47. public string sRemoteName;
  48. public string sComments;
  49. public string sProvider;
  50. }
  51. [DllImport("mpr.dll")]
  52. private static extern int WNetAddConnection2(ref NETRESOURCE oNetworkResource, string sPassword, string sUserName, int iFlags);
  53. public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
  54. {
  55. // 检查最后一个字符是否为\,因为这会导致映射驱动器时出错。
  56. if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
  57. {
  58. sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
  59. }
  60. NETRESOURCE oNetworkResource = new NETRESOURCE();
  61. oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
  62. oNetworkResource.sLocalName = sDriveLetter + ":";
  63. oNetworkResource.sRemoteName = sNetworkPath;
  64. WNetAddConnection2(ref oNetworkResource, null, null, 0);
  65. }

这段代码用于以编程方式映射网络驱动器。

英文:

This blog post suggests that you could use the WNetAddConnection2 Win32 API to map a network drive programmatically:

  1. private enum ResourceScope
  2. {
  3. RESOURCE_CONNECTED = 1,
  4. RESOURCE_GLOBALNET,
  5. RESOURCE_REMEMBERED,
  6. RESOURCE_RECENT,
  7. RESOURCE_CONTEXT
  8. }
  9. private enum ResourceType
  10. {
  11. RESOURCETYPE_ANY,
  12. RESOURCETYPE_DISK,
  13. RESOURCETYPE_PRINT,
  14. RESOURCETYPE_RESERVED
  15. }
  16. private enum ResourceUsage
  17. {
  18. RESOURCEUSAGE_CONNECTABLE = 0x00000001,
  19. RESOURCEUSAGE_CONTAINER = 0x00000002,
  20. RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
  21. RESOURCEUSAGE_SIBLING = 0x00000008,
  22. RESOURCEUSAGE_ATTACHED = 0x00000010
  23. }
  24. private enum ResourceDisplayType
  25. {
  26. RESOURCEDISPLAYTYPE_GENERIC,
  27. RESOURCEDISPLAYTYPE_DOMAIN,
  28. RESOURCEDISPLAYTYPE_SERVER,
  29. RESOURCEDISPLAYTYPE_SHARE,
  30. RESOURCEDISPLAYTYPE_FILE,
  31. RESOURCEDISPLAYTYPE_GROUP,
  32. RESOURCEDISPLAYTYPE_NETWORK,
  33. RESOURCEDISPLAYTYPE_ROOT,
  34. RESOURCEDISPLAYTYPE_SHAREADMIN,
  35. RESOURCEDISPLAYTYPE_DIRECTORY,
  36. RESOURCEDISPLAYTYPE_TREE,
  37. RESOURCEDISPLAYTYPE_NDSCONTAINER
  38. }
  39. [StructLayout(LayoutKind.Sequential)]
  40. private struct NETRESOURCE
  41. {
  42. public ResourceScope oResourceScope;
  43. public ResourceType oResourceType;
  44. public ResourceDisplayType oDisplayType;
  45. public ResourceUsage oResourceUsage;
  46. public string sLocalName;
  47. public string sRemoteName;
  48. public string sComments;
  49. public string sProvider;
  50. }
  51. [DllImport("mpr.dll")]
  52. private static extern int WNetAddConnection2(ref NETRESOURCE oNetworkResource, string sPassword, string sUserName, int iFlags);
  53. public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath)
  54. {
  55. //Checks if the last character is \ as this causes error on mapping a drive.
  56. if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
  57. {
  58. sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
  59. }
  60. NETRESOURCE oNetworkResource = new NETRESOURCE();
  61. oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
  62. oNetworkResource.sLocalName = sDriveLetter + ":";
  63. oNetworkResource.sRemoteName = sNetworkPath;
  64. WNetAddConnection2(ref oNetworkResource, null, null, 0);
  65. }

huangapple
  • 本文由 发表于 2023年5月25日 18:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定