简体   繁体   English

阅读visual studio项目中的文本文档

[英]Read text document within visual studio project

I've added a .txt file with a bunch of text to my project, now I need to read that file and print it in a richtextbox. 我在项目中添加了一个包含大量文本的.txt文件,现在我需要读取该文件并将其打印在richtextbox中。

Any clue how to access the file? 有任何线索如何访问该文件?

You should put it in your resources, accessible under My Project > Resources. 您应该将它放在您的资源中,可以在My Project> Resources下访问。 Choose "Add Existing File" and import your text file. 选择“添加现有文件”并导入文本文件。 Then, you'll be able to access it as a string using the syntax Properties.Resources.<resource name> , which you can assign to your RichTextBox's Text property. 然后,您将能够使用语法Properties.Resources.<resource name>将其作为字符串访问,您可以将其分配给RichTextBox的Text属性。

Alternatively, to read from a file, you can use System.IO.File.ReadAllText if you need the entire contents as a string. 或者,要从文件中读取,如果需要将整个内容作为字符串,则可以使用System.IO.File.ReadAllText

If you don't want to embed the file as resource, you can set Copy to Output Directory to true . 如果您不想将文件作为资源嵌入,则可以将“ Copy to Output Directory设置为true Then you can use File.ReadAllLines(filename) to read all lines from the file as string[] . 然后,您可以使用File.ReadAllLines(filename)从文件中读取所有行作为string[] Then you can set the text with textBox.Text . 然后,您可以使用textBox.Text设置文本。

If you want to embed it, see the other post by @minitech. 如果你想嵌入它,请参阅@minitech的其他帖子。

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

相关问题 在Visual Studio Project中使用文本文件 - Using text files in Visual Studio Project 在Visual Studio中使用StringReader从文本文件读取 - Read from text file with StringReader in Visual Studio 如何在Visual Studio的同一项目中制作多个程序文件 - How to Make Multiple Program Files within Same Project in Visual Studio .NET 4.5中的Visual Studio解决方案中的项目引用 - Project References within a Visual Studio Solution in .NET 4.5 访问Visual Studio项目文件夹中保存的文件 - Accessing a file held within a project folder in Visual Studio Visual Studio中是否有命令来使项目依赖于解决方案中的项目? - Is there a command in Visual Studio to get the projects dependent on a project within a solution? 如何在 Visual Studio 项目中访问其他 SDK - How to get access to additional SDKs within a Visual Studio project 更改Visual Studio 2015扩展中项目中文件ProjectItem的内容吗? - Change the contents of a file ProjectItem within a Project in a Visual Studio 2015 Extension? Visual Studio 2013-如何在解决方案中调试项目 - Visual Studio 2013 - How to debug a project within a solution Visual Studio - Intellisense 无法识别同一解决方案中另一个项目中的命名空间 - Visual Studio - Intellisense not recognizing namespace in another project within same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM