?
Solved

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

Posted on 2005-03-01
4
Medium Priority
?
2,283 Views
Last Modified: 2008-01-09
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
Comment
Question by:matrixmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Expert Comment

by:rugu_16
ID: 13431363
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
 
LVL 27

Expert Comment

by:planocz
ID: 13431386
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
 
LVL 28

Accepted Solution

by:
iboutchkine earned 2000 total points
ID: 13431403
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
 

Author Comment

by:matrixmedia
ID: 13431912
Yeah, that did it.  Thanks!!

as always, its dead simple once you see it.

Tony

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question