C# Path CsvReader - how to use absolute path

0

I'm using System.IO.StreamReader passing a string path (@"‪C:\Users\someuser\somefolder\my.csv")

        using (var reader = new StreamReader(filepath))
        {
            var csvReader = new CsvReader(reader);
            var entries = csvReader.GetRecords<SpreadsheetEntry>();
            return entries.ToList();
        }

this throws the following exception:

System.IO.IOException

HResult=0x8007007B Message=The filename, directory name, or volume label syntax is incorrect :

'C:\Users\someuser\Source\Repos\StakeParser\StakeParser\bin\Debug\netcoreapp2.1\‪C:\Users\someuser\Desktop\testspreadsheet.csv' Source=System.Private.CoreLib StackTrace: at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) .....

My question is, how do i use this correclty so that I can later pass the fullpath as an argument in the console? , at the moment its starting from the working path instead of taking the absoluth path...

c#
csv
path
streamreader
absolute-path
asked on Stack Overflow Sep 30, 2018 by Daniel D • edited Sep 30, 2018 by Daniel D

3 Answers

0

C:\Users\someuser\Source\Repos\StakeParser\StakeParser\bin\Debug\netcoreapp2.1\‪C:\Users\someuser\Desktop\testspreadsheet.csv

Look carefully at the filepath variable StreamReader(filepath) it is 2 paths combined together:

C:\Users\someuser\Source\Repos\StakeParser\StakeParser\bin\Debug\netcoreapp2.1\

C:\Users\someuser\Desktop\testspreadsheet.csv

I am guessing you just want the 2nd part of it. Somewhere before the code shown you likely concatenated the current working directory and a filename, but that filename was already an entire path.

.NET has the Path class the is very useful for handling paths: https://docs.microsoft.com/en-us/dotnet/api/system.io.path

answered on Stack Overflow Sep 30, 2018 by Ves
0

Answer is provided here. Streamreader adds the absolute path to the working path its running at.

http://www.introprogramming.info/tag/streamreader-class/

answered on Stack Overflow Oct 2, 2018 by Daniel D
0

Sometimes C# does not allow to read data from C:\Users... folder.

Use some other folder like D:\SomeFolder.

It will solve the problem.

answered on Stack Overflow Oct 18, 2019 by Sampath Kumara

User contributions licensed under CC BY-SA 3.0