我正在尝试找到一种在 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"
任何人都知道我做错了什么,它似乎离工作如此之近!
谢谢 !