Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and read txt file in ASP

I want to open a txt file and read from it here's the code:

StreamReader reader = File.OpenText("TrackData/vehicle_points.txt");

TrckData is a folder in my web application. but i get this error :

Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\TrackData\vehicle_points.txt'.

My project is not even in C Driver, I don't know where it gets that url.

like image 677
DeadlyDagger Avatar asked Dec 30 '11 09:12

DeadlyDagger


2 Answers

ASP.Net applications are stored in a different folder, and here you have given a relative which may vary according to asp.net executable path.

Please use

 string path = Server.MapPath("TrackData/vehicle_points.txt");
 StreamReader reader = File.OpenText(path);

this will surely work..

like image 151
Kishore Kumar Avatar answered Oct 13 '22 12:10

Kishore Kumar


File.OpenText will start with the path being the current directory - this is the directory the executable is in.

You need to give it the correct full path to the file.

like image 38
Oded Avatar answered Oct 13 '22 11:10

Oded