10

我有一个 Windows 窗体应用程序在不在域上的机器上运行,它需要能够将文件从本地文件系统移动到 UNC 路径。我有该路径的用户名和密码。我想知道有什么方法可以在不执行net.exe命令的情况下直接执行此操作?

理想情况下,我不必映射驱动器。

4

2 回答 2

13

您可以使用 WNetAddConnection 来完成此操作。您将不得不 pInvoke。在我设置 pInvoke 声明后,下面的代码对我有用。第二个代码块(如下)包含 pInvoke 声明——只需将其粘贴到类中即可。


        public static void CopyFile(string from, string shareName, string username, string password)
        {
            NETRESOURCE nr = new NETRESOURCE();
            nr.dwType = ResourceType.RESOURCETYPE_DISK;
            nr.lpLocalName = null;
            nr.lpRemoteName = shareName;
            nr.lpProvider = null;

            int result = WNetAddConnection2(nr,  password,  username, 0);
            System.IO.File.Copy(from, System.IO.Path.Combine(shareName, System.IO.Path.GetFileName(from)));
        }


您需要将以下支持代码粘贴到一个类中(取自 pInvoke.Net)。确保在代码中添加 using 语句:

using System.Runtime.InteropServices

        [DllImport("Mpr.dll", EntryPoint = "WNetAddConnection2", CallingConvention = CallingConvention.Winapi)]
        private static extern int WNetAddConnection2(NETRESOURCE lpNetResource, string lpPassword,  
                                      string lpUsername, System.UInt32 dwFlags);

        [StructLayout(LayoutKind.Sequential)]
        private class NETRESOURCE
        {
            public ResourceScope dwScope = 0;
            public ResourceType dwType = 0;
            public ResourceDisplayType dwDisplayType = 0;
            public ResourceUsage dwUsage = 0;
            public string lpLocalName = null;
            public string lpRemoteName = null;
            public string lpComment = null;
            public string lpProvider = null;
        };

        public enum ResourceScope
        {
            RESOURCE_CONNECTED = 1,
            RESOURCE_GLOBALNET,
            RESOURCE_REMEMBERED,
            RESOURCE_RECENT,
            RESOURCE_CONTEXT
        };

        public enum ResourceType
        {
            RESOURCETYPE_ANY,
            RESOURCETYPE_DISK,
            RESOURCETYPE_PRINT,
            RESOURCETYPE_RESERVED
        };

        public enum ResourceUsage
        {
            RESOURCEUSAGE_CONNECTABLE = 0x00000001,
            RESOURCEUSAGE_CONTAINER = 0x00000002,
            RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
            RESOURCEUSAGE_SIBLING = 0x00000008,
            RESOURCEUSAGE_ATTACHED = 0x00000010,
            RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
        };

        public 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
        };

于 2009-09-17T23:24:47.040 回答
4

这里关于这个问题的公认答案似乎值得研究;它建议使用 Win32 API 函数WNetUseConnection

来自 MSDN:

WNetUseConnection函数与网络资源建立连接该功能可以将本地设备重定向到网络资源。

这似乎完成了你正在寻找的东西,没有提到net.exe. 这有帮助吗?

于 2009-09-17T21:25:53.833 回答