vb.net 如何将自定义文本文件添加到 Visual Studio 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14591429/
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-09 16:38:25  来源:igfitidea点击:

How do I add a custom text file to a visual studio project?

vb.netvisual-studio

提问by Stephen

I'm building a typing application, and would like to add text files containing various commonly used words to the project, which can later be read from. However, when I go to Project -> Add Existing Item, there is only the option to add VB code files to the project. Is there a way to add text files to the project, or will I have to import the data from the file at runtime?

我正在构建一个打字应用程序,并希望将包含各种常用词的文本文件添加到项目中,以后可以从中读取。但是,当我转到 Project -> Add Existing Item 时,只有将 VB 代码文件添加到项目的选项。有没有办法将文本文件添加到项目中,还是必须在运行时从文件中导入数据?

回答by PGallagher

Project Resource (Resx) Version:

项目资源 (Resx) 版本:

This version is slightly easier to work with for your example!

对于您的示例,此版本更容易使用!

  • Right Click on your project
  • Select Properties
  • Goto the Resources Section
  • You will see a drop down in the top left corner of the Resources Secion, shown in the Screenshot below.
  • Select "Files" from the DropDown.
  • Click the Add Resource Button.
  • A Select File Dialog box will be shown.
  • Select your file
  • Save Your Project
  • 右键单击您的项目
  • 选择属性
  • 转到资源部分
  • 您将在资源部分的左上角看到一个下拉菜单,如下面的屏幕截图所示。
  • 从下拉菜单中选择“文件”。
  • 单击添加资源按钮。
  • 将显示一个选择文件对话框。
  • 选择您的文件
  • 保存您的项目

Resx Resources

资源

Now to access this file simply use something like...

现在访问这个文件只需使用类似...

Dim content As String = My.Resources.mytextfile

Note: You don't need the extension, and this will be strongly typed also!

注意:您不需要扩展名,这也将是强类型的!

Embedded Resource Version:

嵌入式资源版本:

  • Right Click on your Project.
  • Goto Add->Existing Item.
  • At the bottom right will be a dropdown (Next to the filename).
  • Select "All Files (*.*)"
  • Browse to your files' location.
  • Add the File.
  • 右键单击您的项目。
  • 转到添加-> 现有项目。
  • 右下角将是一个下拉列表(文件名旁边)。
  • 选择“所有文件 (*.*)”
  • 浏览到您的文件所在的位置。
  • 添加文件。

Select All Files

选择所有文件

Once you have added you file(s), selecting the file(s) in the solution explorer window will allow you to select their "Build Action". You can select either Content, or Embedded Resource from the options.

添加文件后,在解决方案资源管理器窗口中选择文件将允许您选择它们​​的“构建操作”。您可以从选项中选择内容或嵌入式资源。

  • Content: Allows you to retrieve a file (in same dir as assembly) as a stream via Application.GetContentStream( uri ). For this method to work, it needs a AssemblyAssociatedContentFile custom attribute which VS graciously adds when you mark a file as "Content"
  • Embedded resource: embeds the file in an exclusive assembly manifest resource.
  • Content:允许您通过 Application.GetContentStream( uri ) 以流的形式检索文件(与程序集位于同一目录中)。要使此方法起作用,它需要一个 AssemblyAssociatedContentFile 自定义属性,当您将文件标记为“内容”时,VS 会慷慨地添加该属性
  • 嵌入资源:将文件嵌入到独占程序集清单资源中。

Taken from; https://stackoverflow.com/a/145769/1305169

取自; https://stackoverflow.com/a/145769/1305169

If you select Embedded Resource, then to read your text file, you can use code such as;

如果您选择嵌入式资源,那么要读取您的文本文件,您可以使用以下代码;

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

Taken from: http://www.vb-helper.com/howto_net_embedded_resources.html

摘自:http: //www.vb-helper.com/howto_net_embedded_resources.html