[英]How to copy file from remote system to local system using C# (connected in same LAN network)?
Two computers are connected in same LAN network. 两台计算机连接在同一LAN网络中。 I am trying to copy a file from remote system to my local system using C#.
我正在尝试使用C#将文件从远程系统复制到本地系统。 But I cannot able to copy the file from remote system.
但是我无法从远程系统复制文件。 But I can able to make copies in the remote system itself.
但是我可以在远程系统本身中进行复制。
This is to execute WQL (WMI Queries) in the remote system 这是为了在远程系统中执行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 !");
}
But the code is copying file from remote system to local system. 但是代码是将文件从远程系统复制到本地系统。 I can able to make copies in the remote system, but cannot able to copy that to my local system
我可以在远程系统中进行复制,但不能将其复制到本地系统中
Your remote path looks like a UNC path. 您的远程路径看起来像UNC路径。
\\\\REMOTESERVER\\SHARENAME\\Path\\to\\file.txt \\\\ REMOTESERVER \\ SHARENAME \\ Path \\ to \\ file.txt
Where SHARENAME is a shared folder. 其中SHARENAME是共享文件夹。 Windows automatically creates special admin shared folders for the drives on your machine with a $ suffix, so you have C$ mapping to the machines C: drive.
Windows会为带有$后缀的计算机上的驱动器自动创建特殊的管理共享文件夹,因此您具有C $映射到计算机C:驱动器的信息。
Your remote path should therefore be something like: \\\\RemoteComputer\\C$\\Folder1\\Testing\\Test.txt 因此,您的远程路径应类似于:\\\\ RemoteComputer \\ C $ \\ Folder1 \\ Testing \\ Test.txt
You need admin rights on the remote machine to access such a share on a remote machine, or you can create named shares with custom permissions. 您需要远程计算机上的管理员权限才能访问远程计算机上的此类共享,或者您可以创建具有自定义权限的命名共享。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.