【发布时间】:2022-02-22 15:26:56
【问题描述】:
我正在通过 c# 代码访问一个包含 csv 文件的文件夹。我有像“\NL0000NAS0007.dir.xyz.com\webtest\SERVERS\NLWSL086\personnel\people\PROD”这样的网络路径。在使用下面的代码调用时,它会在此 url 前面附加“c:”,因此我无法获取文件。
下面是我的代码 sn-p。
{
try
{
WriteLogFile.WriteLog(this.Configuration.GetValue<string>("logFile"), "Copy CSV File to Server", MessageType.Info);
//string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string projectPath = @"D:\Paracomcsv\";
string folderName = Path.Combine(projectPath, "CsvFiles_" + DateTime.Now.ToString("MM_dd_yyyy"));
string[] files = Directory.GetFiles(sourcePath);
if (!Directory.Exists(folderName))
{
Directory.CreateDirectory(folderName);
}
if (files.Length > 0)
{
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
var fileName = Path.GetFileName(s);
var destFile = Path.Combine(folderName, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
WriteLogFile.WriteLog(this.Configuration.GetValue<string>("logFile"), "File Doesn't Exist", MessageType.Error);
}
}
catch (Exception ex)
{
WriteLogFile.WriteLog(this.Configuration.GetValue<string>("logFile"), ex.Message, MessageType.Error);
throw ex;
}
}
调用 Directory.GetFiles 时出现错误。任何人都有同样的问题,如果是,请告诉我如何调用远程网络共享文件。 谢谢
【问题讨论】:
-
这能回答你的问题吗? Accessing a network file share with C#
-
您的路径应该是 UNC 路径,因此以两个反斜杠开头,而不是一个。
标签: c# .net-core remote-access randomaccessfile