Try it:
protected void btnexecute_Click(object sender, EventArgs e)
{
string pathToCs = Server.MapPath("~/App_Code/Function/Functions/" +
txtfunmoduleName.Text + ".cs"));
ExecuteCode(pathToCs);
}
private void ExecuteCode(string PathToCSFile)
{
string CodeSource = System.IO.File.ReadAllText(PathToCSFile);
string exePath = System.IO.Path.ChangeExtension(PathToCSFile, ".exe");
string assemblypath = GenerateCSharpAssembly(CodeSource, exePath);
if (System.IO.File.Exists(assemblypath))
{
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.UseShellExecute = false;
procInfo.WindowStyle = ProcessWindowStyle.Normal;
procInfo.CreateNoWindow = false;
procInfo.FileName = assemblypath;
procInfo.RedirectStandardOutput = true;
try
{
using (Process exeProcess = Process.Start(procInfo))
{
exeProcess.WaitForExit();
string msg = exeProcess.StandardOutput.ReadToEnd();
Response.Write(msg);
}
}
catch
{
}
}
}
private string GenerateCSharpAssembly(string CodeSource, string exePath)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.CompilerOptions = "/target:exe /platform:x86 /optimize";
compilerParams.GenerateExecutable = true;
compilerParams.GenerateInMemory = false;
compilerParams.OutputAssembly = exePath;
compilerParams.IncludeDebugInformation = false;
compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
compilerParams.ReferencedAssemblies.Add("System.dll");
CompilerResults cr = provider.CompileAssemblyFromSource(compilerParams, CodeSource);
if (cr.Errors.Count > 0)
{
string message = "";
foreach (CompilerError ce in cr.Errors)
{
message += ce.ToString();
}
Response.Write(message);
return "";
}
else
{
return exePath;
}
}