[英]How to copy file from local system to other system in C# (windows app)?
[英]How to copy file from remote system to local system using C# (connected in same LAN network)?
兩台計算機連接在同一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 \\ SHARENAME \\ Path \\ to \\ file.txt
其中SHARENAME是共享文件夾。 Windows會為帶有$后綴的計算機上的驅動器自動創建特殊的管理共享文件夾,因此您具有C $映射到計算機C:驅動器的信息。
因此,您的遠程路徑應類似於:\\\\ RemoteComputer \\ C $ \\ Folder1 \\ Testing \\ Test.txt
您需要遠程計算機上的管理員權限才能訪問遠程計算機上的此類共享,或者您可以創建具有自定義權限的命名共享。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.