.NET 核心文件传输到网络位置



我有将文件从一个位置传输到网络驱动器的代码。源位置可能是我的电脑或其他网络驱动器。我正在使用.NET Core 2.1和C#。 我的代码类似于下面MSDN示例中的代码。

问题是,当目标是我PC上的文件夹时,这工作正常,但是当它是如下所示的网络位置时,文件不会移动到指定位置。源代码中的文件确实被删除了,并且没有任何错误。

我已确保我登录时使用的 Windows 帐户具有对网络位置的显式权限。我假设这是我的应用程序运行的上下文。我还环顾四周并尝试进行模拟以显式使用具有权限的帐户,并找到了一些代码来执行此操作,但似乎这在 .net core 中不起作用。

我错过了什么?我怎样才能让它工作?

string fileName = @"TestFile.txt";
string sourcePath = @"C:usersmyuserdocs";
string targetPath =  @"\10.10.10.148docs";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and 
// overwrite the destination file if it already exists.
System.IO.File.Move(sourceFile, destFile, true);

将移动命令放在 try catch 块中以查看返回的实际错误(如果有(。

try
{
// To copy a file to another location and 
// overwrite the destination file if it already exists.
System.IO.File.Move(sourceFile, destFile, true);
}
catch (Exception ex)
{
//show error message using appropriate method for 
//Console Application
Console.WriteLine(ex.Message);
//OR Web Application //
Response.Write(ex.Message);
//OR WinForms Application
MessageBox.Show(ex.Message);
}

然后,您将获得更多信息来帮助进行故障排除。

最新更新