[英]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.