C# 将文件复制到另一台计算机上的共享文件夹
我正在尝试将文件复制到映射的网络位置。 如果我尝试手动操作,一切正常 通过运行以下代码,我不会得到任何异常,但我不会在需要的位置得到代码C# 将文件复制到另一台计算机上的共享文件夹,c#,.net,C#,.net,我正在尝试将文件复制到映射的网络位置。 如果我尝试手动操作,一切正常 通过运行以下代码,我不会得到任何异常,但我不会在需要的位置得到代码 string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests"; if (Directory.Exists(_sharedLocation) && File.Exists(@
string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
if (Directory.Exists(_sharedLocation) && File.Exists(@"c:\\Automation\\Tests\\Test1\\events.json"))
{
File.Copy(@"c:\\Automation\\Tests\\Test1\\events.json", Path.Combine(_sharedLocation, "events11.json"), true);
}
string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
关于该问题的任何建议。似乎目标路径
\u共享位置
也指本地路径,而不是远程路径。我猜您的共享位置路径无效
如果您写@“c:\”,它将引用运行代码的本地驱动器
因此,请更正它查看
\u SharedLocation
变量,它位于以下位置:“…\Windows\Network Shortcuts\…”
我在这里只是猜测,但您是否尝试引用网络文件夹的快捷方式,而不是网络文件夹
这是行不通的:
File.Copy(myOriginalFile, "C:\...\MyShortcutToANetworkFolder\myFile.txt");
为什么不呢?因为快捷方式基本上是一个文件,而不是一个文件夹(它比那个更复杂,但为了论证起见,我保持它的简单)。不能将文件(或任何其他内容)放入快捷方式。使用快捷方式唯一能做的就是打开它
您需要实际的网络文件夹路径
这将有助于:
File.Copy(myOriginalFile, "\\myServer\myFolder1\myFolder2\myFile.txt");
问题:您的共享路径指的是同一台机器的
C:
驱动器。可能您指的是映射网络位置的快捷方式
string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
if (Directory.Exists(_sharedLocation) && File.Exists(@"c:\\Automation\\Tests\\Test1\\events.json"))
{
File.Copy(@"c:\\Automation\\Tests\\Test1\\events.json", Path.Combine(_sharedLocation, "events11.json"), true);
}
string _sharedLocation = @"C:\Users\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
应该是:
string _sharedLocation = @"\\ComputerNetworkIdentity\SharedFolder\pddd\AppData\Roaming\Microsoft\Windows\Network Shortcuts\system-tests";
可以使用\\ComputerName
定位共享计算机。
您必须对共享文件夹
具有写入权限
找到共享文件夹的简单方法:
Run
对话框\\ComputerNetworIdentity
你确定你不仅仅是在其他地方捕捉到了一个异常吗?目标文件夹是否需要身份验证?查看可能会为您提供一个俱乐部查看或文件。无论该路径指向本地还是远程位置,复制都应该有效。我认为问题的根源在于,给定的文件路径是文件夹快捷方式,而不是实际的文件夹。