开发者

streamreader not reading from textfilewithin the project

I am using the st开发者_StackOverflow中文版reamreader 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????


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜