You can add the file as a resource to your project. See
here[
^]. :)
Also if you plan to make a
Setup and Deployment project for your application you can add your files in this project and copy them during installation.
[UPDATE]
Check out
this[
^]
MSDN article. It demonstrates both how to embed files and how to access files from resources.
Here is a quick demo using the technique described in the article.
I've created a new windows forms application and added a
"Snake.exe" file to this application. After that I changed the Build Action property (from properties of the newly added resource) to Embed Resource (it is described in the article). And here is the code for accessing the resource and writing it to file:
try
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("MyNameSpace.Resources.Snake.exe");
if(stream != null)
{
using (FileStream fileStream = new FileStream("C:\\Snake.exe", FileMode.Create))
{
using (BinaryWriter bw = new BinaryWriter(fileStream))
{
byte[] buffer = ReadAllBytes(stream);
bw.Write(buffer);
bw.Close();
}
}
}
}
catch
{
MessageBox.Show("Error accessing resources!");
}
In the above code I call a helper method (
ReadAllBytes
) to read the stream content to a byte buffer. Here is the code for this method:
public static byte[] ReadAllBytes(Stream input)
{
byte[] buffer = new byte[1024 * 8];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
I hope this demo is useful for you. If you encounter some problems, you can always check the embedded resource names with
Assembly.GetManifestResourceNames[
^] or using
ILDASM. :)
[/UPADTE]