»¶Ó­À´µ½ÎÒÃǵļÒ

C#±àÒëÆ÷

ÒÔÏÂÊǹúÍâÈËдµÄÒ»¸öC#±àÒëÆ÷£¬µ±È»ÓÉÓÚËûÒÀȻʹÓõÄ.NetϵÄFCL£¬ËùÒÔ²»¿ÉÄÜÊÇÕæÕýÒâÒåÉϵıàÒëÆ÷£¬ ·Éµ¶ÎÒ½éÉܳöÀ´£¬Ö»ÊÇ¿ÉÒÔÈôó¼Ò°ÚÍÑcscµÄÔ¼Êø£¬ÔÚWin½çÃæÉÏÍê³É±àÒë.


Äã±ØÐëÒÔÏÂÃæµÄ²½ÖèÍê³É£º

1.½¨Á¢Ò»¸öCSharpCodeProvider ʵÀý(Èç¹ûÊÇʹÓÃVisual BasicÔòʹÓÃVBCodeProvider)
2.°üº¬½Ó¿ÚICodeCompiler
3.ÌṩCompilerParametersµÄ²ÎÊý
4.ʹÓÃCompileAssemblyFromSource·½·¨±àÒë¡£
5.ÔËÐÐCompilerResults
6.Ö´ÐбàÒëºÃµÄ³ÌÐò


±àÒëµÄ´úÂë¿ÉÒÔÊÇдÔÚÎı¾¿òÖеÄ×Ö·û´®£¬µ±È»Ò²¿ÉÒÔÔ´Îļþ¡£

ÒÔÏÂÊÇÔ´´úÂ룺)£¬¹ýÁ½ÌìÎÒÐÞ¸ÄһϺóÌṩÕû¸öÔ´´úÂ룺)

private void button1_Click(object sender, System.EventArgs e)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
// For Visual Basic Compiler try this :
//Microsoft.VisualBasic.VBCodeProvider

ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters();

parameters.GenerateExecutable = true;
if (appName.Text == "")
{
System.Windows.Forms.MessageBox.Show(this,
"Application name cannot be empty");
return ;
}

parameters.OutputAssembly = appName.Text.ToString();

if (mainClass.Text.ToString() == "")
{
System.Windows.Forms.MessageBox.Show(this,
"Main Class Name cannot be empty");
return ;
}

parameters.MainClass = mainClass.Text.ToString();
parameters.IncludeDebugInformation = includeDebug.Checked;

// Add available assemblies - this should be enough for the simplest
// applications.
foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
parameters.ReferencedAssemblies.Add(asm.Location);
}

String code = textBox1.Text.ToString();
//System.Windows.Forms.MessageBox.Show(this, code);

CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
code);

if (results.Errors.Count > 0)
{
string errors = "Compilation failed: ";
foreach (CompilerError err in results.Errors)
{
errors += err.ToString() + " ";
}
System.Windows.Forms.MessageBox.Show(this, errors,
"There were compilation errors");
}
else
{
#region Executing generated executable
// try to execute application
try
{
if (!System.IO.File.Exists(appName.Text.ToString()))
{
MessageBox.Show(String.Format("Cant find {0}", appName),
"Cant execute.", MessageBoxButtons.OK,
MessageBoxIcon.Error);
return;
}
ProcessStartInfo pInfo = new ProcessStartInfo(appName.Text.ToString());
Process.Start(pInfo);
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Error while executing {0}",
appName) + ex.ToString(), "Cant execute.",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}

#endregion

}

}






#If you have any other info about this subject , Please add it free.#
êdzÆ:
ÓÊÏä:
µç»°:

ÄúµÄÁôÑÔ:


Èç¹ûÄúÓйØÓÚ C#±àÒëÆ÷ ·½ÃæµÄ²úÆ·»òÕß¿´·¨£¬¾´ÇëÔÚ´Ë·¢²¼.