远程WMI复制文件夹 [英] Remote WMI copy folder

查看:221
本文介绍了远程WMI复制文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法,一个文件夹复制到网络共享(家庭车程)在C#中使用WMI。我需要能够通过用户的凭据,因为他们是谁可以访问该文件夹的唯一的。这里是我到目前为止所。

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.

方法:

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



我正在reciving该错误是在这一行

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 !

谢谢!

推荐答案

在你的情况下,未找到只是意味着目录中是找不到的。

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

最有可能的问题是,你正试图从远程计算机访问该目录的同时指定UNC路径。因为你已经连接到远程计算机,路径应该是在当地的格式为:

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

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

这篇关于远程WMI复制文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆