I have created an Editor. In That editor i am calling WriteLog Method, Which is hard coded in my program, with some string value. There is Button for With Compile Your Program Text. On that Click I am trying to save a file with name Variable.cs. Than From my code I am Calling C# Compiler at the same time to produce result.
This Is My Code to Save File
private void SaveCSFile()
{
MyCSFile = string.Empty;
MyCSFile = @"using System;
class Variable"
+ "{";
MyCSFile += ConvertGrid(dataGridView1);
MyCSFile += ConvertGrid(dataGridView2);
MyCSFile += @"public Variable()" +
"{";
MyCSFile+= T2.Text +
"}";
MyCSFile += @"public void WriteLog(String Msg)
{
Console.WriteLine(Msg);
Console.ReadLine();
}
public string toString(string msg)
{
return msg.ToString();
}
}";
MyCSFile += @"class MainClass
{";
MyCSFile+=@"static void Main()" + "\n" +
"{";
MyCSFile += @"Variable v=new Variable();";
MyCSFile += "v."+T1.Text+ "\n";
MyCSFile += "}}";
MessageBox.Show(MyCSFile);
writer = System.IO.File.CreateText(FilePath);
writer.WriteLine(MyCSFile.Trim()+System.Environment.NewLine);
writer.Close();
}
This The Code Where I am Calling C# Compiler And Passing the Source File
private void GenerateScript()
{
SaveCSFile();
string AppName = tabControl3.SelectedTab.Text + ".exe";
string mainClass = tabControl3.SelectedTab.Text;
CSharpCodeProvider code = new CSharpCodeProvider();
ICodeCompiler compiler = code.CreateCompiler();
CompilerParameters param = new CompilerParameters();
param.GenerateInMemory = true;
param.OutputAssembly = AppName.ToString();
param.MainClass = mainClass.ToString();
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
param.ReferencedAssemblies.Add(asm.Location);
}
string Mycode = MyCSFile ;
CompilerResults result = compiler.CompileAssemblyFromFile(param, "Variable.cs");
if (result.Errors.Count > 0)
{
string error = "Compilation error :\n";
foreach (CompilerError err in result.Errors)
{
error += err.ErrorText+" "+"In Line Number :"+err.Line +" ";
}
MessageBox.Show(this, error, "Error");
}
else
{
#region Try to ExecuteApplication
try
{
if (!System.IO.File.Exists(AppName))
{
MessageBox.Show("FileName" + AppName + " Not Found");
return;
}
ProcessStartInfo pinfo = new ProcessStartInfo
{
FileName = AppName,
Arguments = FilePath,
UseShellExecute=false
};
Process.Start(AppName, FilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
#endregion
}
}
here I call WriteLog Method And Passing a string value like this
WriteLog("Hello World");
Form a notepad i created.. But i am getting error every time
"The specified executable is not a valid application for this OS platform"
pls help
Thank you.....