[英]Remote WMI copy folder
我正在尝试找到一种在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 + "'");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.