首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >远程WMI复制文件夹

远程WMI复制文件夹
EN

Stack Overflow用户
提问于 2013-04-27 05:25:43
回答 1查看 2.3K关注 0票数 2

我正在尝试找到一种在C#中使用WMI将文件夹复制到网络共享(家庭驱动器)的方法。我需要能够传递用户凭据,因为他们是唯一可以访问文件夹的人。这是我到目前为止所拥有的。

方法:

代码语言:javascript
复制
static uint DirectoryCopy(string computer, string user, string pass, string SourcePath, string DestinationPath, bool Recursive)
    {
                        try
            {
                ConnectionOptions connection = new ConnectionOptions();
                connection.Username = user;
                connection.Password = pass;
                connection.Impersonation = ImpersonationLevel.Impersonate;
                connection.EnablePrivileges = true;
                ManagementScope scope = new ManagementScope(
                    @"\\" + computer + @"\root\CIMV2", connection);
                scope.Connect();



                ManagementPath managementPath = new ManagementPath(@"Win32_Directory.Name=" + "\'" + SourcePath.Replace("\\", "\\\\") + "\'");

                ManagementObject classInstance = new ManagementObject(scope, managementPath, null);

                // Obtain in-parameters for the method

                ManagementBaseObject inParams =
                    classInstance.GetMethodParameters("CopyEx");

                // Add the input parameters.
                inParams["FileName"] = DestinationPath.Replace("\\", "\\\\");
                inParams["Recursive"] = true;
                inParams["StartFileName"] = null;

                // Execute the method and obtain the return values.
                ManagementBaseObject outParams =
                    classInstance.InvokeMethod("CopyEx", inParams, null);

                // List outParams

                MessageBox.Show((outParams["ReturnValue"]).ToString());


            }
            catch (UnauthorizedAccessException)
            {
                lblBackupStatus.Text = "Access Denied, Wrong password for selected user";
            }

            catch (ManagementException exc)
            {
                MessageBox.Show(exc.ToString());
            }
    }

以及我传递给该方法的内容:

代码语言:javascript
复制
        string computer = ddlBackupselectcomp.Text;
        string user = ddlBackupselectuser.Text;
        string pass = txtBackuppwd.Text;

        string userdesktop =  @"\\" + computer + @"\C$\Users\" + user + @"\Desktop";

        string hdrivepath = @"\\dist-win-file-3\homes\" + user;



            string SourcePath = userdesktop;
            string DestinationPath = hdrivepath;

            DirectoryCopy(computer, user, pass, SourcePath, DestinationPath, true);

我收到的错误在这行上

代码语言:javascript
复制
ManagementBaseObject inputArgs = dir.GetMethodParameters("CopyEx"); "Not Found"

任何人都知道我做错了什么,它看起来是如此接近工作!

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-27 05:54:00

在你的例子中,"Not found“仅仅意味着找不到目录。

最有可能的问题是,在指定UNC路径的同时,您正在尝试从远程计算机访问目录。由于您已连接到远程计算机,因此路径应为本地格式:

代码语言:javascript
复制
string userdesktop =  @"c:\Users\" + user + @"\Desktop";

代码语言:javascript
复制
ManagementPath managementPath = new ManagementPath(@"Win32_Directory.Name='" + SourcePath + "'");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16245461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档