473,396 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

howto embed a textfile into an exe

Jan
Hi,

What I want is to add a text file to my project which will be
included/embedded in my exe file.
So not a link to a text file in a folder but a text file physical in the exe.

Is this possible..? How can I access the content of the file..?

Jan
Nov 17 '05 #1
2 4967
Yes this is possible. Simply add the file to your solution, set the
properties of the file withon the project to "Embedd Resource" and load the
file at runtime.

You will have to use a fully qualified name for the file such as
"MyAssembly.MyFolder.MyFile.txt" and you can use the
Assembly.GetManifestResourceStream to fetch the file into a stream that you
can read.

For a similar example of how to embed a font into an application see the
GDI+ FAQ. This has all the principles, you can just change the details to
deal with the text file case.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Jan" <Ja*@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
Hi,

What I want is to add a text file to my project which will be
included/embedded in my exe file.
So not a link to a text file in a folder but a text file physical in the
exe.

Is this possible..? How can I access the content of the file..?

Jan

Nov 17 '05 #2
Hi,

Add the file to the project, select it in the project explorer, click
properties, select "embedded resource"
then use this code to extract it:

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jan" <Ja*@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
Hi,

What I want is to add a text file to my project which will be
included/embedded in my exe file.
So not a link to a text file in a folder but a text file physical in the
exe.

Is this possible..? How can I access the content of the file..?

Jan

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
0
by: ATS | last post by:
HOWTO Extract resources from .NEt User Control and save them to file. Please help, I have a .NET C# User Control (derrived from System.Windows.Forms.UserControl), and I want to embed into it...
0
by: ATS | last post by:
HOWTO Make a UserControl deploy an embedded resource. Please help, I need to embed an EXE into a C# UserControl that is run from script in an HTML web page as such: <html> <object...
8
by: flippetigibbet | last post by:
I was thinking that if I create: \documents and settings\user\My Documents\my\scripts\py\dirname\__init__.py and a .pth file in site-packages with something like: \documents and settings\user\My...
15
by: Encapsulin | last post by:
Hello everybody. I'm trying to change src of quicktime embedded object with javascript: <html><body> <script language="JavaScript"> function Exchange() { document.qtvr.src = "sample2.pano";...
6
by: Encapsulin | last post by:
Hello everybody, is it possible to hide qtvr <object...> (or even change its size to 1 pixel rectangle)? I need to hide qtvr from the page dynamically, if .mov source is empty. For example: ...
1
by: Susanne West | last post by:
i'm desperately looking for a php code example on how to embed an image (jpg,...) into an id3-tag of an mp3. has anyone seen/done something like that?
0
by: dcollier9 | last post by:
Hi ! I need to embed a text box in a footer of a GridView's boundfield column to get data prior to a row insert. The textbox is usable and showing up in the web page source using View
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.