views:

232

answers:

2

I am using the streamreader to read from a file in the project..

StreamReader stRead = new StreamReader("textfile.txt");

        while (!stRead.EndOfStream)
        {
            CheckBoxList1.Items.Add(stRead.ReadLine());
        }

but i get an error:

Could not find file 'c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\textfile.txt'.

whereas the textfile is in my bin folder of the project.

C:\Users\xyz\Documents\Visual Studio 2008\Projects\WebApplication3\WebApplication3\bin\testfile.txt

If i use this path it works but i dont want to use the complete path...

Is there a way to do this???

thanks

how do i store the path as a reference in web.config file????

+1  A: 

I would try the project root directory, otherwise, define the path prefix in a configuration file like your web.config, that way if you're worried about path changes it isn't hard-coded

f0ster
how do i define path prefix in config file??thanks
Depending on what kind of environment you're going to deploy your app on, it might be easier to do something like Tanathos mentioned above,Otherwise you can put a key in your web.config (xml file) in the appSettings add a key like<add key="Your path" value="C:\whereever">Then reference it in your code like string fullpath = ConfigurationManager.AppSettings["Your Path"] + "\filename.txt"; Using this style can be convenient if you map different paths in yoru web.config's, or you have to deal with certain permission setups on different deployment environments
f0ster
+3  A: 

Maybe you must use

StreamReader stRead = new StreamReader(Server.MapPath("~/textfile.txt"));

to resolve the physical position of your file, supposing it's placed in the root of your project.

tanathos