两台计算机连接在同一LAN网络中。我正在尝试使用C#将文件从远程系统复制到本地系统。但我无法从远程系统复制文件。但我可以在远程系统中制作副本。
这是在远程系统中执行WQL(WMI查询)
ObjectQuery query = new ObjectQuery("SELECT * FROM CIM_DataFile WHERE
FileName='test' AND Extension='txt'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
queryCollection = searcher.Get();
ManagementBaseObject inParams, outParams;
string localPath = "C:\\Users\\UserName\\Desktop\\log.txt";
string remotePath = "\\\\RemoteComputer\\C:\\Folder1\\Testing\\test.txt";
foreach (ManagementObject m in queryCollection)
{
// Display the remote file information in local system
Console.WriteLine("Name :{0}", m["Name"]);
Console.WriteLine("File status :{0}", m["Status"]);
Console.WriteLine("File Type :{0}", m["FileType"]);
Console.WriteLine("Object Name :" + m.ToString());
Console.WriteLine("\nFile copying has been initiated ...");
// Method - 1
using (FileStream localDest = File.OpenWrite(localPath))
using (FileStream remoteSource = File.OpenRead(remotePath))
{
remoteSource.CopyTo(localDest);
}
// Method - 2
File.Copy(remotePath, localPath, true);
// Method - 3
inParams = m.GetMethodParameters("Copy");
inParams["FileName"] = localPath;
outParams = m.InvokeMethod("Copy", inParams, null);
Console.WriteLine("Return value :" + outParams["ReturnValue"]);
Console.WriteLine("\nFile copying completed !");
}
但代码是将文件从远程系统复制到本地系统。我可以在远程系统中制作副本,但无法将其复制到本地系统
您的远程路径看起来像UNC路径。
\\ REMOTESERVER名\共享\路径\为\ file.txt的
SHARENAME是共享文件夹的位置。 Windows会自动为您机器上的驱动器创建一个带有$后缀的特殊管理员共享文件夹,因此您可以将C $映射到计算机C:驱动器。
因此,您的远程路径应为:\\ RemoteComputer \ C $ \ Folder1 \ Testing \ Test.txt
您需要远程计算机上的管理员权限才能访问远程计算机上的此类共享,或者您可以使用自定义权限创建命名共享。