gpt4 book ai didi

c# - 远程 WMI 复制文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:15 24 4
gpt4 key购买 nike

我试图找到一种方法,使用 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"

任何人都知道我做错了什么,它似乎非常接近工作!

谢谢!

最佳答案

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

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

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

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

关于c# - 远程 WMI 复制文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16245461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com