Follow me on Twitter RSS FEED

C# Programmatically Compiling .cs files to executables

I was thinking of making a program that generates an executable which installs an application, but how to generate the executable?

public static void Compile(String Output, String code)
{
    CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
    CompilerParameters parameters = new CompilerParameters();
    //Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = true;
    parameters.OutputAssembly = Output;
    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, code);
    if (results.Errors.Count > 0)
    {
        foreach (CompilerError CompErr in results.Errors)
        {
            Debug.WriteLine(
                "Line number " + CompErr.Line +
                ", Error Number: " + CompErr.ErrorNumber +
                ", '" + CompErr.ErrorText + ";" +
                Environment.NewLine + Environment.NewLine);
        }
    }
    else
    {
        Debug.WriteLine("Success!");
    }
}

In order to use this function, you must using these two classes:

using System.CodeDom.Compiler;
using System.Diagnostics;

Once you have that done, you're good to go! As a quick example, you can create a textarea named txt_code, a textbox named txt_output, and a button btn_submit. When btn_submit is clicked, then it will say:

Compile(txt_output.Text, txt_code.Text);

Which if you put a valid path in txt_output, and valid C# code in txt_code, then you will have your executable!

Original Article Here

Google Search Stories

Google came out with Google Search Stories, where you can make a video of a series [up to 7] of google searches to tell a short story. I made one to test it out:


You can make your own here:
http://www.youtube.com/searchstories

C# Remote Desktop Client

Recently, I've been trying to find a challenging C# Project to work on. I've tried stuff like a program that tells you your WPM and then submits it to the highscore table. It wasn't really very challenging, although I did learn a thing or two. But the other day, I really hit the nail on the head: A C# Remote Desktop Host/Client.

Skip this paragraph if you already know what a Remote Desktop Host/Client is
A remote desktop host/client is a program that lets one person remotely control another person's desktop. So, for example, lets say I had a friend in california, and I live in north carolina. The person in california, using this program, could control my desktop and, say, create and populate a text document. The host is the program that lets you control the desktop that the client program is running on.

So in order to pull this off, I need to have two connections: One for the server to send information to the host, and one for the client to send information to the host. I was initially going to use Sockets, but that would only work over LAN. So I created some PHP scripts to communicate with the MySQL database to do things such as post a string to the output database [the input for the other side], and check for posts to the input database [the output for the other side]. Once I got all that working, I could use WebClient.DownloadString("Script.php"); to execute the script via C#. Both Get and Post parameters work with this.

Then I set up threads on the client and the host to check for messages multiple times per second. The main problem at this point was sending an Image, 1440x900px [my screen resolution], to the database quickly. Uploading and Downloading it directly would be far to slow, so I did a google search for "C# Image Serialization". I found a really good method, using base64 encoding, and it uploads fast. So the client would take a screenshot, encode it to base64, and send it to the database via PHP. Then the server would download the encoded image as a string, decode it into an image again, and display it. All this happening multiple times per second.

So all that is the theory behind it. I still have to implement the sending and receiving, but everything else is working properly.