Embed Files in to a C# Executable

10 Jun

I needed to run some regedits on several computers and grab the IP Addresses. I was given all the scripts to run and had 2 options: include all the regedits in my C# code or execute the BAT that runs them all. I chose option 2 because I did not write them and wanted to keep them separate from my code. Also, I could not have users downloading multiple files or unzipping anything. So I chose to embed the files in an EXE, extract them when it runs, execute the BAT, email the IP, and delete them when done. I found the pieces of this code on StackOverflow.

To embed a file, you need to add it to your project in Visual Studio then under properties choose “embedded resource”.

Here is the full code. After you can find the code stripped to the bare minimum:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Diagnostics;
using System.Reflection;
using System.IO;

namespace TSUnet
{
public partial class Form1 : Form
{
Assembly _assembly;
Stream _move;
Stream _move1;
Stream _move2;
Stream _move3;
Stream _move4;
Stream _cmd;
string os;
string printos;
public Form1()
{
InitializeComponent();
if (System.IO.File.Exists(“Quest_Move.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move.reg”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_1.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_1.reg”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_2.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_2.reg”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_3.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_3.reg”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_4.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_4.reg”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
if (System.IO.File.Exists(“QuestMove.cmd”))
{

try
{
System.IO.File.Delete(“QuestMove.cmd”);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
return;
}
}
}

private void button1_Click(object sender, EventArgs e)
{
string name = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(name);
IPAddress[] addr = ipEntry.AddressList;

try
{
_assembly = Assembly.GetExecutingAssembly();
_move = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move.reg”);
_move1 = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move_1.reg”);
_move2 = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move_2.reg”);
_move3 = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move_3.reg”);
_move4 = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move_4.reg”);
_cmd = _assembly.GetManifestResourceStream(“TSUnet.QuestMove.cmd”);

using (Stream file = File.OpenWrite(“Quest_Move.reg”))
{
CopyStream(_move, file);
}
using (Stream file = File.OpenWrite(“Quest_Move_1.reg”))
{
CopyStream(_move1, file);
}
using (Stream file = File.OpenWrite(“Quest_Move_2.reg”))
{
CopyStream(_move2, file);
}
using (Stream file = File.OpenWrite(“Quest_Move_3.reg”))
{
CopyStream(_move3, file);
}
using (Stream file = File.OpenWrite(“Quest_Move_4.reg”))
{
CopyStream(_move4, file);
}
using (Stream file = File.OpenWrite(“QuestMove.cmd”))
{
CopyStream(_cmd, file);
}

os = Environment.OSVersion.ToString();
if (os.Contains(“5.”))
{
printos = addr[addr.Length – 1].ToString();
}
else
{
printos = addr[3].ToString();
}

 

SmtpClient smtpserver = new SmtpClient(“Something.Something.Com”);
MailMessage mail = new MailMessage();
mail.From = new MailAddress(textBox1.Text.ToString());
mail.To.Add(“Me@Something.Com”);
mail.Subject = “From: ” + textBox1.Text.ToString() + ” Machine: ” + Environment.MachineName.ToString();
mail.Body = textBox1.Text.ToString() + “\r\n” + printos + “\r\n” + Environment.MachineName;

smtpserver.Send(mail);
Process.Start(“QuestMove.cmd”);
}

 
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), “Error”);
}
MessageBox.Show(“Please save your data, Log Off your machine and Call….”, “Done”);
if (System.IO.File.Exists(“Quest_Move.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move.reg”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_1.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_1.reg”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_2.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_2.reg”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_3.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_3.reg”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}
if (System.IO.File.Exists(“Quest_Move_4.reg”))
{

try
{
System.IO.File.Delete(“Quest_Move_4.reg”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}
if (System.IO.File.Exists(“QuestMove.cmd”))
{

try
{
System.IO.File.Delete(“QuestMove.cmd”);
}
catch (System.IO.IOException er)
{
Console.WriteLine(er.Message);
return;
}
}

}
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}

 

And a very minimal version – a single embed without emailing or getting the IP:

namespace TSUnet

{
public partial class Form1 : Form
{
Assembly _assembly;
Stream _move;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
_assembly = Assembly.GetExecutingAssembly();
_move = _assembly.GetManifestResourceStream(“TSUnet.Quest_Move.reg”);

using (Stream file = File.OpenWrite(“Quest_Move.reg”))
{
CopyStream(_move, file);
}

}

catch (Exception ex)
{
MessageBox.Show(ex.ToString(), “Error”);
}}

public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: