2

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

方法:

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());
            }
    }

我传递给该方法的内容:

        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);

我收到的错误在这条线上

ManagementBaseObject inputArgs = dir.GetMethodParameters("CopyEx"); "Not Found"

任何人都知道我做错了什么,它似乎离工作如此之近!

谢谢 !

4

1 回答 1

1

在您的情况下,“未找到”仅表示未找到该目录。

最有可能的问题是您在指定 UNC 路径时尝试远程计算机访问目录。因为您已经连接到远程机器,所以路径应该是本地格式:

string userdesktop =  @"c:\Users\" + user + @"\Desktop";

ManagementPath managementPath = new ManagementPath(@"Win32_Directory.Name='" + SourcePath + "'");
于 2013-04-26T21:53:59.887 回答