Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2342
  • Last Modified:

Reading an embedded resource into a string variable with vb.net

What I am having trouble reading a text file (actually, a SQL script) into my VB.Net app.  I have it set as an embedded resource, and its been added to the project, however I cannot seem to figure out how to retrieve it now that its been embedded.

Ive seen a lot of solutions on how to do this with images, and none with how to do it as text.

Thank you in advance for your help.

-Tony
0
matrixmedia
Asked:
matrixmedia
1 Solution
 
rugu_16Commented:
Hi Tony,

Can you provide more information like how are you embedding it and what
kind of text file or sql script is this. Also what is the specific error ?

Peace
0
 
planoczCommented:
Here is a sample that I had for images and text files....


'FORM 1

Imports System.io
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.Text = "Form1"
    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim executing_assembly As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly()

        ' Get the namespace.
        Dim my_namespace As String = executing_assembly.GetName().Name.ToString()


        'Load image to a dynamically created picture box.

        Dim picture_stream As Stream
        Dim bm As Bitmap

        picture_stream = executing_assembly.GetManifestResourceStream(my_namespace + ".1040.ico") '<--to your icon image

        If Not (picture_stream Is Nothing) Then
            bm = New Bitmap(picture_stream)
            Dim pict As New PictureBox
            pict.Image = bm
            pict.Top = 5
            pict.Left = 5
            pict.Height = 50
            pict.Width = 50
            pict.Visible = True
            Me.Controls.Add(pict)
            picture_stream.Close()
        End If



        'Load text to a dynamically created Label.

        Dim text_stream As Stream = executing_assembly.GetManifestResourceStream(my_namespace _
                   + ".Test.txt")   '<--- a text file
        If Not (text_stream Is Nothing) Then
            Dim stream_reader As New StreamReader(text_stream)
            Dim lbl As New Label
            lbl.Text = stream_reader.ReadToEnd()
            lbl.Top = 75
            lbl.Left = 5
            lbl.Width = 500
            lbl.Height = 500
            lbl.Visible = True
            Me.Controls.Add(lbl)
            stream_reader.Close()
        End If

    End Sub
End Class
0
 
iboutchkineCommented:
To read a text file, the program makes a StreamReader, passing the
stream to its constructor.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    ' Get our assembly.
    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
End Sub
0
 
matrixmediaAuthor Commented:
Yeah, that did it.  Thanks!!

as always, its dead simple once you see it.

Tony

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now