0

我正在使用流读取器从项目中的文件中读取..

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

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

但我得到一个错误:

找不到文件“c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\textfile.txt”。

而文本文件位于项目的我的 bin 文件夹中。

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

如果我使用这条路径它可以工作,但我不想使用完整的路径......

有没有办法做到这一点???

谢谢

我如何将路径作为参考存储在 web.config 文件中????

4

2 回答 2

3

也许你必须使用

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.

于 2009-11-09T22:44:00.087 回答
1

我会尝试项目根目录,否则,在 web.config 之类的配置文件中定义路径前缀,这样如果您担心路径更改,它就不会被硬编码

于 2009-11-09T21:45:33.700 回答