Reading a Text File within a project

D

Dan

Hi VB Gurus,
I know how to read/write file from a path.

But I have a text file witnin my project, and don't know how to read it. The
file is embedded within a project.
I'll appreciate any help/suggestion/advice I get.

Thanks, Dan
 
M

Mr. Arnold

Dan said:
Hi VB Gurus,
I know how to read/write file from a path.

But I have a text file witnin my project, and don't know how to read it. The
file is embedded within a project.
I'll appreciate any help/suggestion/advice I get.

Thanks, Dan

That file is going to need to be moved to a physical location and read
from that location. The exe or whatever is going to be deployed
somewhere that is not a part of the development project. And you're
going to have to deploy that file to a location of where the executable
is located a read there.
 
O

Onur Güzel

Hi VB Gurus,
I know how to read/write file from a path.

But I have a text file witnin my project, and don't know how to read it. The
file is embedded within a project.
I'll appreciate  any help/suggestion/advice I get.

Thanks, Dan

Hi,

First let's assume you have a text file named "TextFile1.txt" in your
project and let your project's name and root namespace name be
"WindowsApplication1".

Set TextFile1.txt's "build action" property to "Embedded Resource" as
it should be.

Then you can get the text of embedded text file using
GetManifestResourceStream as follows:

'---------------------------------------------------------
Dim mystream As System.IO.Stream = _
System.Reflection.Assembly. _
GetExecutingAssembly.GetManifestResourceStream _
("WindowsApplication1.TextFile1.txt")

Using reader As New IO.StreamReader(mystream)
' Read the contents in MsgBox for example
MsgBox(reader.ReadToEnd)
End Using
'---------------------------------------------------------

Hope it helps,

Onur Güzel
End Using
 
D

Dan

Thanks Onur.
Your example helped me a lot.

Thanks again.
Dan

Hi VB Gurus,
I know how to read/write file from a path.

But I have a text file witnin my project, and don't know how to read it.
The
file is embedded within a project.
I'll appreciate any help/suggestion/advice I get.

Thanks, Dan

Hi,

First let's assume you have a text file named "TextFile1.txt" in your
project and let your project's name and root namespace name be
"WindowsApplication1".

Set TextFile1.txt's "build action" property to "Embedded Resource" as
it should be.

Then you can get the text of embedded text file using
GetManifestResourceStream as follows:

'---------------------------------------------------------
Dim mystream As System.IO.Stream = _
System.Reflection.Assembly. _
GetExecutingAssembly.GetManifestResourceStream _
("WindowsApplication1.TextFile1.txt")

Using reader As New IO.StreamReader(mystream)
' Read the contents in MsgBox for example
MsgBox(reader.ReadToEnd)
End Using
'---------------------------------------------------------

Hope it helps,

Onur Güzel
End Using
 
W

Webmaster

Hello Dan,

You can get the startup path by doing this
Code "Application.StartupPath + "\" + filename"
Then you can also use the "Process.Start" to start the file or use your
script to read the file.
If this wasn't the answer you were expecting, ask your question again and
start with telling wich version of VB your using.
Greetz..

Jeroen Visser
Programmer in VB.net, HTML and PHP
www.gunneweg-paintings.com
I'm 14 Years old but my programming isn't bad!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top