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.
- 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:
TransferData.TransferData td = new TransferData.TransferData();
and then write the data with:
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:
TransferData.TransferData td = new TransferData.TransferData();
and then read the data with:
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.