Tuesday, 1 July 2008

Runing Executable Files in ASP .NET

In this post I will provide you with a code snippet that runs an executable file (.exe) on server side.
For this you need to first declare an object of System.Diagnostics.Process type.
Then set few parameters and then start executing!

System.Diagnostics.Process batch =
new System.Diagnostics.Process();
batch.StartInfo.FileName =
Server.MapPath("~/YourExeFile.exe");
batch.StartInfo.Arguments =
String.Format("Arguments go here");
batch.StartInfo.CreateNoWindow = true;
//if you do not want
//to display the window...
batch.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;

//now you can start the job...
batch.Start();

//if you want to wait until
//the process terminates,
//you can use the following line:
batch.WaitForExit();


Also note that in order to be able to execute files, you need to have some permissions, here you can find more about security permissions.

You can use this snippet in conjunction with my previous post to build some thing like YouTube ;)

After the user uploads a new video (MPG, AVI and etc.) you can convert the uploaded file to .flv file using this code and FFmpeg tool.

This is a short code for converting videos:

batch.StartInfo.FileName =
Server.MapPath("~/ffmpeg.exe");
batch.StartInfo.Arguments =
String.Format(@"-i ""{0}\t.mpg""
""{0}\t.flv"" -y",
Server.MapPath("~"));