Ste*_*hen 5 vb.net visual-studio
我正在构建一个打字应用程序,并希望将包含各种常用单词的文本文件添加到项目中,以后可以从中读取.但是,当我转到Project - > Add Existing Item时,只有选项可以将VB代码文件添加到项目中.有没有办法将文本文件添加到项目中,还是我必须在运行时从文件中导入数据?
PGa*_*her 13
项目资源(Resx)版本:
对于您的示例,此版本更容易使用!

现在访问此文件只需使用类似...
Dim content As String = My.Resources.mytextfile
Run Code Online (Sandbox Code Playgroud)
注意:您不需要扩展名,这也将是强类型的!
嵌入式资源版本:

添加文件后,在解决方案资源管理器窗口中选择文件将允许您选择"构建操作".您可以从选项中选择内容或嵌入式资源.
- 内容:允许您通过Application.GetContentStream(uri)检索文件(与程序集在同一目录中)作为流.要使此方法起作用,它需要一个AssemblyAssociatedContentFile自定义属性,当您将文件标记为"内容"时,VS会慷慨地添加该属性
- 嵌入式资源:将文件嵌入独占程序集清单资源中.
如果选择Embedded Resource,那么要读取文本文件,可以使用如下代码;
Dim executing_assembly As System.Reflection.Assembly = _
Me.GetType.Assembly.GetEntryAssembly()
' Get our namespace.
Dim my_namespace As String = _
executing_assembly.GetName().Name.ToString()
' Load a text file.
Dim text_stream As Stream = _
executing_assembly.GetManifestResourceStream(my_namespace _
+ ".text1.txt")
If Not (text_stream Is Nothing) Then
Dim stream_reader As New StreamReader(text_stream)
Label1.Text = stream_reader.ReadToEnd()
stream_reader.Close()
End If
Run Code Online (Sandbox Code Playgroud)
取自:http://www.vb-helper.com/howto_net_embedded_resources.html