【问题标题】:Access files from Network shared folder从网络共享文件夹访问文件
【发布时间】: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 时出现错误。任何人都有同样的问题,如果是,请告诉我如何调用远程网络共享文件。 谢谢

【问题讨论】:

标签: c# .net-core remote-access randomaccessfile


【解决方案1】:

代码 sn-p 不能是完整的源代码。目前还不清楚变量是如何初始化的。但是鉴于您的网络路径“\NL0000NAS0007.dir.xyz.com\webtest\SERVERS\NLWSL086\personnel\people\PROD”很明显,它不起作用。该路径以单个 \ 字符开头,因此 System.IO API 假定您的意思是当前目录或驱动器的相对路径。这可能是“C:”...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多