Remote WMI copy folder

2019-05-21 07:46发布

问题:

I'm trying to find a way to copy a folder to a network share (homes drive) using WMI in C#. I need to be able to pass the users credentials as they are the only ones who can access the folder. Here is what i have so far.

Method:

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

And what i pass to the method:

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

The error i'm reciving is on this line

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

Anyone know what i'm doing wrong, it seems like its so close to working !

Thanks !

回答1:

In your case "Not found" simply means that the directory is not found.

Most likely the problem is that you are trying accessing the directory from the remote computer while specifying the UNC path. Because you are already connected to the remote machine, the path should be in the local format:

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

and

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


标签: c# wmi