System.IO.FileNotFoundException:主项目文件无法被IIS Express找到求助
Hey there, I’ve run into this exact issue before—let’s break down what’s happening and how to fix it fast.
The Root Problem
When you run your ASP.NET project with IIS Express, the working directory defaults to IIS Express’s installation folder (that C:\Program Files (x86)\IIS Express\ path in your error message), not your project’s root directory. So when you use just Cobb_County_Traffic_Counts.csv as the path, the code is looking in the wrong place entirely.
Solution 1: Use Server.MapPath (Best for Web Forms Pages)
Server.MapPath converts a relative path (relative to your project root) to the actual physical path on the server. This is perfect for code in your page’s code-behind:
Modify your loadCSV method to use this instead of the hardcoded relative path:
protected void loadCSV(object sender, EventArgs e) { // ~/ points directly to your project's root directory string filePath = Server.MapPath("~/Cobb_County_Traffic_Counts.csv"); List<String[]> fileContent = new List<string[]>(); // Rest of your file reading code goes here... }
Don’t forget to double-check your CSV file’s properties:
- Right-click the CSV in Visual Studio → Properties
- Set Copy to Output Directory to Copy if newer or Copy always
This ensures the file gets copied to your project’s output folder when you build, so it’s available at runtime.
Solution 2: Use AppDomain.CurrentDomain.BaseDirectory (For Non-Page Classes)
If your loadCSV method is in a separate class (not directly in a page’s code-behind), use this to get your app’s base directory:
using System.IO; using System; // ... string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cobb_County_Traffic_Counts.csv");
This points to your project’s root (or bin folder, depending on your setup) and will correctly locate the file.
Quick Check
After making these changes, run your project again—your code should now look in the right place for the CSV file instead of IIS Express’s folder.
内容的提问来源于stack exchange,提问作者Austin

