Using a .txt file Resource in VB

后端 未结 3 1485
野趣味
野趣味 2020-12-10 18:42

Right now i have a line of code, in vb, that calls a text file, like this:

Dim fileReader As String
    fileReader = My.Computer.FileSystem.ReadAllText(\"dat         


        
相关标签:
3条回答
  • 2020-12-10 19:03

    First, go to project resources (My Project --> Resources), and drag-and-drop your file, say "myfile.txt", from the file system to the resourses page. Then:

        Imports System.IO
        ...
        Dim stream As New MemoryStream(My.Resources.myfile)
        Dim reader As New StreamReader(stream)
        Dim s As String = reader.ReadToEnd()
    
    0 讨论(0)
  • 2020-12-10 19:08

    If you added the file as a resource in the Project + Properties, Resources tab, you'll get its content by using My.Resources:

    Dim content As String = My.Resources.data5
    

    Click the arrow on the Add Resource button and select Add Existing File, select your data5.txt file.

    0 讨论(0)
  • 2020-12-10 19:16

    I'm assuming that the file is being compiled as an Embedded Resource.

    Embedded Resources aren't files in the filesystem; that code will not work.

    You need to call Assembly.GetManifestResourceStream, like this:

    Dim fileText As String
    Dim a As Assembly = GetType(SomeClass).Assembly
    Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt"))
        fileText = reader.ReadToEnd()
    End Using
    
    0 讨论(0)
提交回复
热议问题