读取存储在项目中的文本文件 [英] Read Text File which is store in project

查看:64
本文介绍了读取存储在项目中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗口应用程序中添加了文本文件.我想读写该文本文件.
当我启动我的应用程序时,我会写入该文本文件,但是当我停止我的应用程序时,当我再次启动我的项目时,所有数据将从我的文本文件中删除.
我使用此代码读取文本文件.每次都不进入while循环.
任何人都可以解决这个问题.
StreamReader objStreamReader;
objStreamReader = File.OpenText("Manual_pro_ID.txt");
字符串行= string.Empty;
while((line = objStreamReader.ReadLine())!= null)
{
sb.Append(line);
sb.Append(Environment.NewLine);
}
objStreamReader.Close();

我只想读写存储在我的解决方案资源管理器中的文本文件.
这样就完成了所有功能,但是我在文件路径中只是个问题,我没有得到文本文件的正确路径.因此,如果您知道,请告诉我该文件的路径.

I add text file in my window application.And i want to read and write that text file.
I write into that text file when i start my application but when i stop my application and when i start again my project then all data remove from my text file.
I use this code for reading text file.And every time not go in to while loop.
Can any one solve this.
StreamReader objStreamReader;
objStreamReader = File.OpenText("Manual_pro_ID.txt");
string line = string.Empty;
while ((line = objStreamReader.ReadLine()) != null)
{
sb.Append(line);
sb.Append(Environment.NewLine);
}
objStreamReader.Close();

I just want to read and write text file which is store in my solution explorer.
So done all function but i just have problem in file path i am not get correct path of my text file.So if you know then tell me which path i take for that file.

推荐答案

使用System.IO;

StreamReader objReader =新的StreamReader(Application.StartupPath +"\\ text.txt");
字符串内容= objReader.ReadToEnd();
using System.IO;

StreamReader objReader = new StreamReader(Application.StartupPath + "\\text.txt");
string content = objReader.ReadToEnd();


还有另一个与文件路径有关的问题.

实际上,在任何情况下都无法对文件名进行硬编码.这段代码的问题是这样的:它假定打开文件时的工作目录将是您的文本文件所在的目录.这是错误的:用户可以从任何目录开始运行任何应用程序.在这种情况下,将找不到该文件.

如果文件是只读文件,则可能会将其放在可执行文件所在的输出目录中.因此,您需要计算此目录.方法如下:

There is one additional problem related to the file path.

Actually, there are no situations when hard-coding of the file name can be useful. The problem with this code is this: it assumes that the working directory at the moment of opening the file will be the one where your text file is located. This is wrong: the user can run any application starting from any directory. In this case, the file will not be found.

If the file is read-only, it''s possible that you put it in your output directory, the same one where your executable file is located. So, you need to calculate this directory. Here is how:

string exeDirectory = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location);



另一种选择是将文件添加到项目中,但将其作为资源嵌入到可执行文件中.在这种情况下,您无需阅读任何内容. Visual Studio将使用已包含文本文件内容的静态字符串属性创建自动生成的源文件.该文件不会被复制到输出目录,因为可执行文件包含它的内容.

—SA



Another option is to add the file to the project but embed it in the executable file as a resource. In this case, you don''t have to read anything. Visual Studio will create auto-generated source file with the static string property already containing the content of the text file. The file will not be copied to output directory as executable file contains it content.

—SA


这篇关于读取存储在项目中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆