Click here to Skip to main content
15,883,940 members
Articles / Programming Languages / C#

Transferring Data to the New EXE

Rate me:
Please Sign up or sign in to vote.
4.00/5 (12 votes)
13 Aug 2009CPOL3 min read 30.5K   336   20   14
Have you ever wanted to change variables without rebuilding?
Image 1

Introduction

Changing one or more variables in your application without even rebuilding might sometimes be a lifesaver for developers and users. Let's assume that you coded an application which uploads files to a server but you want to enable your users to write their server info on their own. You probably can do this by adding a registry key or an application setting. But what if the user wants this application to be portable, to be used on other PCs or not to save anything to the PC. With this code, I am going to explain it in this article and you will be able to do all these. 

In order to use this code, you only need to include the "TransferData.cs" to your project and create an instance of it. After creating an instance of the class, it is actually straight forward thanks to IntelliSense.

Anyway, I included a demonstration Editor (which makes changes on the stub) and the output stub. Output stub is basically your application. 

Background 

I needed a solution like this to use in my project and I couldn't find a satisfactory solution, so I coded it myself and I thought someone might need this code someday. So, here it is.

Using the Code

How It Works?

  • Editor reads the output stub from the resource and appends the separator and then the data you entered. PE files aren't affected when you append data at the end of the file, therefore your program will work without a problem. If you open the output file and the stub file on hex editor and compare them, you will see something like this. If you want, you can encrypt the string before you write it to the file.

    Image 2

  • At runtime, your output reads the data from end of file and processes it. This way, you can change the variables in your application without the need of rebuilding it or make your users be able to customize your application. There are infinite fields of use.

Editor Side (Writing the Data) 

The editor writes the variables (of course as one string) to the end of the file so that the application will be able to read them at runtime. As most of you know, you can append data to the end of an PE file and the PE file will work without a problem. In order to write data to the output, you should first create an instance of the class with:

C#
TransferData.TransferData td = new TransferData.TransferData();

and then write the data with:

C#
td.Write(Resource.Output, "output-new.exe", strToWrite, strSeperator); 

Resource.Output is your output stub which you included into your project, the rest is self explanatory. 

Application Side (Reading the Data)

Your application reads the variables from itself at runtime. In order to get the transferred data, you need to create an instance of the class with:

C#
TransferData.TransferData td = new TransferData.TransferData();

and then read the data with:

C#
td.Read(Application.ExecutablePath, strSeperator);

Read method returns the data as string so that you can work on it easily. If the application is not able to read the data correctly, it doesn't throws an exception. It just returns an "ERROR OCCURED" string. An important point is that the separator string (strSeperator) should be the same as the separator string you used in the editor.

Points of Interest

Here are some things that you should be careful about when using this code:

  • Only use ASCII characters in your data.
  • Your separator and data should not exceed 200 characters. You can actually change this limit by changing the buffer length on Read() method. Its length should be LIMIT+20.
  • Your data should never contain the separator in itself.

History

  • 14th August, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood share! Pin
alrsds19-Sep-09 20:52
alrsds19-Sep-09 20:52 
GeneralA few things need to be discussed... Pin
supercat914-Aug-09 5:13
supercat914-Aug-09 5:13 
GeneralGreat!May be we can use it do other thing Pin
guaike14-Aug-09 3:44
guaike14-Aug-09 3:44 
GeneralRe: Great!May be we can use it do other thing Pin
SimpleData14-Aug-09 3:45
SimpleData14-Aug-09 3:45 
GeneralMy vote of 1 Pin
Michael E. Jones13-Aug-09 23:23
Michael E. Jones13-Aug-09 23:23 
GeneralRe: My vote of 1 Pin
Jefis13-Aug-09 23:38
Jefis13-Aug-09 23:38 
GeneralRe: My vote of 1 Pin
SimpleData13-Aug-09 23:41
SimpleData13-Aug-09 23:41 
GeneralRe: My vote of 1 Pin
Michael E. Jones14-Aug-09 0:16
Michael E. Jones14-Aug-09 0:16 
GeneralRe: My vote of 1 Pin
SimpleData14-Aug-09 0:18
SimpleData14-Aug-09 0:18 
GeneralRe: My vote of 1 Pin
Michael E. Jones14-Aug-09 1:04
Michael E. Jones14-Aug-09 1:04 
GeneralRe: My vote of 1 Pin
Johnno7417-Aug-09 16:10
Johnno7417-Aug-09 16:10 
GeneralRe: My vote of 1 Pin
Tom Spink18-Aug-09 10:34
Tom Spink18-Aug-09 10:34 
AnswerMy vote + Pin
bagum4-Sep-09 20:39
bagum4-Sep-09 20:39 
GeneralRe: My vote + Pin
Tom Spink4-Sep-09 23:17
Tom Spink4-Sep-09 23:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.