Search code examples
c#.net-coreremote-accessrandomaccessfile

Access files from Network shared folder


I am accessing one folder with csv file under it by c# code. I have network path like "\NL0000NAS0007.dir.xyz.com\webtest\SERVERS\NLWSL086\personnel\people\PROD". While calling with below code it is appending "c:" earlier to this url so I am not able to get the files.

Below is my code snippet.

        {
                      
            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;
            }
        }

I am getting the error while calling Directory.GetFiles. Anyone had the same issue, if yes please let me know how to call remote network shared file. Thanks


Solution

  • The code snippet cant be the full source code. It is unclear how the variables are initialised. But given your network path of "\NL0000NAS0007.dir.xyz.com\webtest\SERVERS\NLWSL086\personnel\people\PROD" it is very clear, that it doesnt function. The path starts with a single \ character, so the System.IO API assumes you mean a relative path to whatever is the current directory or drive . And this could be "C:"...