2
我試圖找到一種方法來使用C#中的WMI將文件夾複製到網絡共享(家庭驅動器)。我需要能夠傳遞用戶憑據,因爲他們是唯一可以訪問該文件夾的人。這是我到目前爲止。遠程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);
我正在reciving的錯誤是在這條線
ManagementBaseObject inputArgs = dir.GetMethodParameters("CopyEx"); "Not Found"
任何人都知道我」做錯了,看起來就像它的工作非常接近!
謝謝!
哦,是有道理的,現在,我會做任何事情到源路徑或的DestinationPath?現在我有一個彈出的消息框,並說出於某種原因9。任何想法爲什麼?非常感謝。 – Boundinashes6 2013-04-26 22:22:37
9是** CopyEx **方法的返回值,意思是「無效對象」。在你的情況下,FileName參數值肯定是格式錯誤的(嘗試使用目標路徑的格式)或者目標路徑無法訪問 – 2013-04-26 22:29:18