I am getting the error:
Could not find file 'C:\\Program Files (x86)\\IIS Express\\Cobb_County_Traffic_Counts.csv'.'
My file is in the main project file (as shown below), so I don't know how it cannot be found.
Here is the code I am running in entirety:
private const string Path = (@"Cobb_County_Traffic_Counts.csv");
protected void Page_Load(object sender, EventArgs e)
{
loadCSV(sender, e);
}
protected void loadCSV(object sender, EventArgs e)
{
List<String[]> fileContent = new List<string[]>();
using (StreamReader reader = new StreamReader(Path)) // mind the encoding - UTF8
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.TrimWhiteSpace = true; // if you want
parser.Delimiters = new[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
string[] line = parser.ReadFields();
fileContent.Add(line);
Console.WriteLine(line);
}
}
}
Please use Server.MapPath()
to get the actual path. You are resolving the file to IIS installation directory because it is a relative path to where IIS express executable is running from.
List<String[]> fileContent = new List<string[]>();
using (StreamReader reader = new StreamReader(Server.MapPath("~/" + Path))) // mind the encoding - UTF8
using (TextFieldParser parser = new TextFieldParser(reader))
{
parser.TrimWhiteSpace = true; // if you want
parser.Delimiters = new[] { "," };
parser.HasFieldsEnclosedInQuotes = true;
while (!parser.EndOfData)
{
string[] line = parser.ReadFields();
fileContent.Add(line);
Console.WriteLine(line);
}
}
}
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.