简体   繁体   中英

Console Application: System.IO.FileNotFoundException: Could not find file

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");

Cars项目截图

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?

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)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM