如何在C#项目中访问txt文件? [英] How to access a txt file in the c# project?

查看:139
本文介绍了如何在C#项目中访问txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual c#2010 express
我希望能够在Windows Forms Apllication或WPF浏览器应用程序中添加文件
如何将文本文件添加到项目中,然后访问其内容并进行更改
我想用它在我的网站上建立评分系统
当有人给费率打分时,应将费率添加到文本文件中...

文本文件应集成在项目本身中.

I am using Visual c# 2010 express
I want to be able to add a file in a Windows Forms Apllication or WPF browzer application
how do I add a text file to the project and then access its contents and change them
I want to use this to make a rating system in my website
when someone rates the rate should be added to the text file...

the text file should be integrated in the project itself

推荐答案

创建文本文件.
只需右键单击您的项目并添加添加文件",即可将其添加到您的项目中.

在需要时通过代码修改文件.

最后,确保它在设置中消失.
Create a text file.
Just add it to your project by right clicking on your project and adding "add files".

Modify the file as and when you require via code.

Finally, make sure it goes out in the setup.


为其提供资源.将新资源文件和现有文本文件都添加到项目中,在资源编辑器(列出了资源类型)中,使用添加现有文件".您不必阅读资源的内容.请注意,C#代码将在资源节点下自动生成.打开此代码;您会发现一个静态字符串属性(通常在名称旁边关闭文本文件的名称).

这样,文本文件将集成到项目本身,但不会复制到输出中,而是嵌入到可执行文件中.

但是,如果要复制文件,则可以简单地将文本文件添加到项目中,转到节点的属性并指定其生成行为(例如,始终复制").在这种情况下,您将需要通过常规IO访问文件.

我强烈建议在资源中使用嵌入,而不要使用IO.

—SA
Make it a resource. Add both new resource file and existing text file to the project, in the resource editor (where resource types are listed) use "Add existing file". You don''t have to read the content of the resource. Pay attention, a C# code will be auto-generated under the resource node. Open this code; you will find a static string property (usually under the name close the the name of your text file).

In this way, the text file is integrated to the project itself, but not copied to the output but embedded in the executable.

If you want the file to be copied, though, you can simply add the text file to the project, go to the node''s properties and specify its build behavior ("Copy always", for example). In this case, you will need to access the file via regular IO.

I would highly recommend to use embedding in the resource, not using IO.

—SA


这篇关于如何在C#项目中访问txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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