远程WMI复制文件夹

我正在尝试使用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 + "'");