-1

I have a .NET console application (.NETFramework v.4.8) that reads a CSV file in the root of my project.

File.ReadAllLines("fuel.csv");

Screenshot of Cars project

When I run my program, I get the following error:

System.IO.FileNotFoundException: Could not find file 
"/Users/ryan.payne/Sites/linq-fundamentals/LinqFundamentals/Cars/bin/Debug/fuel.csv"

How do I get my console application to read the file?

Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
  • 1
    All right, but did you follow, in the first link, how they manage to read that file once it is embedded? – Steve Dec 16 '20 at 22:38
  • @Steve I realized that those directions are for a Xamarin app and not for a console application. I clarified that part of my question. I've resolved the issue and posted my findings in: https://stackoverflow.com/a/65332151/11809808. – Ryan Payne Dec 16 '20 at 22:42

1 Answers1

1

You have two options:

Option 1 - Manually edit .csproj file

Open the .csproj file and replace <None Include="fuel.csv"> with the following:

<None Include="fuel.csv">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

Option 2 - Update properties in dialog

  1. Right click on the file
  2. Click "Properties"
  3. Set "Copy to output directory" to "Copy if newer" (scroll down if you don't see these options)

enter image description here

Ryan Payne
  • 5,249
  • 4
  • 28
  • 69
  • 1
    Alternatively you can read from resource when you decide to embed it as resource, https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Lex Li Dec 16 '20 at 22:44
  • Not sure about Rider, but in Visual Studio you can select the file in the solution explorer tree and then set the property 'Copy To Output Directory' there, no need to open the .csproj file. I'm guessing Rider may have the same ability. – quaabaam Dec 17 '20 at 00:41
  • @quaabaam Unfortunately, that feature is not available in Rider. ☹️ – Ryan Payne Dec 17 '20 at 16:16
  • @quaabaam Turns out the feature is available in Rider. I missed the scrollbar and didn't realize there were more options. I've updated my answer accordingly. – Ryan Payne Dec 18 '20 at 22:56