vb.net 如何读取资源中的 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28263094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:45:38  来源:igfitidea点击:

How to read .txt file in Resources

vb.netwinformsiotext-files

提问by user3740891

Found a question already answering this? Please link it. I've not found anything.

发现一个问题已经回答了这个问题?请链接。我什么也没找到。

How do I read a .txtfile which I've placed into my Resourcesfolder? The following code doesn't seem to find the file, even I know I've put it there. Maybe I'm doing something wrong?

如何读取.txt已放入Resources文件夹的文件?下面的代码似乎没有找到该文件,即使我知道我已经把它放在那里了。也许我做错了什么?

Using textreader As System.IO.TextReader = File.OpenText("Resources/map.txt")

This doesn't seem to work. Neither does:

这似乎不起作用。也没有:

Using textreader As System.IO.TextReader = File.OpenText(My.Resources.Map)

(Mapis the name I've given it inside Visual Studio).

Map是我在 Visual Studio 中给它起的名字)。

Alternatively, how would I find the program's Debug/Resourcesfolder within the file system? What method do I use?

或者,我如何Debug/Resources在文件系统中找到程序的文件夹?我使用什么方法?

Any ideas what I can do to solve this? And when someone is using my program and wants to save the map file, where would be best programming practice to save it? (Since I can't put it in Resourcesbecause that's within the .exe). Just C:\Programs\ A new folder just for my program?

有什么想法可以解决这个问题吗?当有人使用我的程序并想要保存地图文件时,保存它的最佳编程实践是什么?(因为我不能把它放进去,Resources因为那在 内.exe)。只是 C:\Programs\ A new folder just for my program

N.B. VB.NET WinForms

NB VB.NET WinForms

回答by JStevens

I believe this question was best answer previously here: StackOverflow Question/Answer

我相信这个问题是以前最好的答案: StackOverflow Question/Answer

Dim content As String = My.Resources.Map

回答by user3029051

I'm using Visual Studio 2015 and I needed to find a way to read text files within my program. This is what I found: To add an existing text file as a resource Right Click on [project Name - your project] in solution explorer, then Select Properties, then Resources, Then add the text file as an existing text file (e.g. - you create myfile.txt and put it where you can find it) You can then read the text file as a stream of lines or as a single string

我正在使用 Visual Studio 2015,我需要找到一种方法来读取我的程序中的文本文件。这是我发现的:将现有文本文件添加为资源在解决方案资源管理器中右键单击 [项目名称 - 您的项目],然后选择属性,然后选择资源,然后将文本文件添加为现有文本文件(例如 - 您创建 myfile.txt 并将其放在您可以找到的位置)然后您可以将文本文件作为行流或单个字符串读取

To read a resource text file as a stream of lines. This example will display the first line. You can then use streamreader code to access all the lines.

将资源文本文件作为行流读取。此示例将显示第一行。然后,您可以使用流阅读器代码访问所有行。

Dim lineA As String = String.Empty
Using reader As TextReader = New StringReader(My.Resources.myfile)
lineA = reader.ReadLine
MsgBox(lineA)
End Using

To read the resource text file as a single string and display (e.g.) the first 5 characters

将资源文本文件作为单个字符串读取并显示(例如)前 5 个字符

Dim LineB as String = MyResources.myfile
MsgBox(Mid(LineB,1,5))