简体   繁体   中英

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.

Any clue how to access the file?

You should put it in your resources, accessible under 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.

Alternatively, to read from a file, you can use System.IO.File.ReadAllText if you need the entire contents as a string.

If you don't want to embed the file as resource, you can set Copy to Output Directory to true . Then you can use File.ReadAllLines(filename) to read all lines from the file as string[] . Then you can set the text with textBox.Text .

If you want to embed it, see the other post by @minitech.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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