简体   繁体   English

在Visual Studio Project中使用文本文件

[英]Using text files in Visual Studio Project

I am making a console program in VS2010. 我在VS2010中制作一个控制台程序。 The structure of as layed out in the solution explorer is Project file, Properties folder, References, and Program.cs. 解决方案资源管理器中布局的结构是Project文件,Properties文件夹,References和Program.cs。

I want to make use of a text file for reading/writing. 我想利用文本文件进行读取/写入。 What is the correct way of adding this file to the project? 将此文件添加到项目的正确方法是什么? If I just do StreamWriter sw = new StreamWriter("maze.txt") then it'll create the file in the output folders (bin/debug or bin/release). 如果我只是执行StreamWriter sw = new StreamWriter("maze.txt")那么它将在输出文件夹(bin / debug或bin / release)中创建文件。 But it won't show up in the solution explorer. 但是它不会显示在解决方案资源管理器中。

Now if I right-click on the solution explorer and Add New Item, I can create a text file in the root project folder (same level as Program.cs). 现在,如果我右键单击解决方案资源管理器并添加新项,则可以在根项目文件夹(与Program.cs级别相同)中创建一个文本文件。 This will show up in the solution explorer. 这将显示在解决方案资源管理器中。

Is there a way for me to access this newly created file? 有没有办法让我访问这个新创建的文件? Other than doing something like StreamWriter sw = new StreamWriter("../../maze.txt") by specifying the path to be two parents up? 除了通过将路径指定为两个父对象之外,还要做类似StreamWriter sw = new StreamWriter("../../maze.txt")

How am I supposed to manage external files in a Visual Studio console applicatoin? 我应该如何在Visual Studio控制台应用程序中管理外部文件? In a WinForms application, there's a resources folder where I can add these things and a Resources.resx file to manage it. 在WinForms应用程序中,有一个resources文件夹,我可以在其中添加这些内容,以及一个Resources.resx文件来管理它。 I can access it with Properties.Resources.someres . 我可以使用Properties.Resources.someres访问它。

If you want to create a file at design time and have it included in your bin folder then add the text file, go to properties, and select "copy always" or "copy if newer" for the copy property. 如果要在设计时创建文件并将其包含在bin文件夹中,请添加文本文件,转到属性,然后为copy属性选择“始终复制”或“如果更新则复制”。

Note that this will copy the file from the project to the bin folder, but changes in the bin folder when debugging won't be copied up into the project itself. 请注意,这会将文件从项目复制到bin文件夹,但是调试时bin文件夹中的更改不会复制到项目本身。 I doubt you want the program to interact with the version of the file in the project itself. 我怀疑您是否希望该程序与项目本身中的文件版本进行交互。 If you do something like that then anytime you run the program outside of VS (ie when you copy it to the machine of an actual user) it won't work. 如果您执行这样的操作,那么无论何时只要您在VS外部运行程序(即,将其复制到实际用户的计算机上),该程序都将无法工作。

As for your resources question , you can still use resources. 至于您的资源问题 ,您仍然可以使用资源。 Just right-click and "Add" -> "New Item" (aka keyboard shortcut Ctrl+Shift+A) and choose "Resources File". 只需右键单击并单击“添加”->“新建项目”(又称键盘快捷键Ctrl + Shift + A),然后选择“资源文件”。 You could also set the text file to be an Embedded Resource . 您还可以将文本文件设置为嵌入式资源 MSDN has a lot of information on using resources on their Managing Application Resources page . MSDN在其“ 管理应用程序资源”页面上有很多有关使用资源的信息。

You can still embed resources to a console application. 您仍然可以将资源嵌入控制台应用程序。 I've used this technique in quick apps so we don't need a installer or deploy dependencies such as these type of files. 我在快速应用程序中使用了此技术,因此我们不需要安装程序或部署诸如此类文件的依赖项。 This question discusses a similar topic. 这个问题讨论了类似的话题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM