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:\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
Answer is provided here. Streamreader adds the absolute path to the working path its running at.
Sometimes C# does not allow to read data from C:\Users... folder.
Use some other folder like D:\SomeFolder.
It will solve the problem.
User contributions licensed under CC BY-SA 3.0