如何在Win界面上完成C#编译

本文只是可以让大家摆脱csc的约束,在Win界面上完成C#编译编译.

在C#编译过程中你必须以下面的步骤完成:

1.建立一个CSharpCodeProvider 实例(如果是使用Visual Basic则使用VBCodeProvider)

2.包含接口ICodeCompiler

3.提供CompilerParameters的参数

4.使用CompileAssemblyFromSource方法编译。

5.运行CompilerResults

6.执行C#编译好的程序

编译的代码可以是写在文本框中的字符串,当然也可以源文件。

 
 
 
  1. private void button1_Click(object   
  2. sender, System.EventArgs e)   
  3. {   
  4. CSharpCodeProvider codeProvider =   
  5. new CSharpCodeProvider();   
  6. // For Visual Basic Compiler try this :   
  7. //Microsoft.VisualBasic.VBCodeProvider   
  8.  
  9. ICodeCompiler compiler =   
  10. codeProvider.CreateCompiler();   
  11. CompilerParameters parameters =   
  12. new CompilerParameters();   
  13.  
  14. parameters.GenerateExecutable = true;   
  15. if (appName.Text == "")   
  16. {   
  17. System.Windows.Forms.MessageBox.Show(this,   
  18. "Application name cannot be empty");   
  19. return ;   
  20. }   
  21.  
  22. parameters.OutputAssembly = appName.  
  23. Text.ToString();   
  24.  
  25. if (mainClass.Text.ToString() == "")   
  26. {   
  27. System.Windows.Forms.MessageBox.Show(this,   
  28. "Main Class Name cannot be empty");   
  29. return ;   
  30. }   
  31.  
  32. parameters.MainClass =  
  33. mainClass.Text.ToString();   
  34. parameters.IncludeDebugInformation =   
  35. includeDebug.Checked;   
  36.  
  37. // Add available assemblies - this   
  38. should be enough for the simplest   
  39. // applications.   
  40. foreach (Assembly asm in AppDomain.  
  41. CurrentDomain.GetAssemblies())   
  42. {   
  43. parameters.ReferencedAssemblies.  
  44. Add(asm.Location);   
  45. }   
  46.  
  47. String code = textBox1.Text.ToString();   
  48. //System.Windows.Forms.MessageBox.  
  49. Show(this, code);   
  50.  
  51. CompilerResults results =   
  52. compiler.CompileAssemblyFromSource  
  53. (parameters, code);   
  54.  
  55. if (results.Errors.Count > 0)   
  56. {   
  57. string errors = "Compilation failed:\n";   
  58. foreach (CompilerError err   
  59. in results.Errors)   
  60. {   
  61. errors += err.ToString() + "\n";   
  62. }   
  63. System.Windows.Forms.MessageBox.  
  64. Show(this, errors,   
  65. "There were compilation errors");   
  66. }   
  67. else   
  68. {   
  69. #region Executing generated executable   
  70. // try to execute application   
  71. try   
  72. {   
  73. if (!System.IO.File.Exists(appName.  
  74. Text.ToString()))   
  75. {   
  76. MessageBox.Show(String.Format("Can't   
  77. find {0}", appName),   
  78. "Can't execute.", MessageBoxButtons.OK,   
  79. MessageBoxIcon.Error);   
  80. return;   
  81. }   
  82. ProcessStartInfo pInfo =   
  83. new ProcessStartInfo(appName.Text.ToString());   
  84. Process.Start(pInfo);   
  85. } it55.com   
  86. catch (Exception ex)   
  87. {   
  88. MessageBox.Show(String.Format(  
  89. "Error while executing {0}",   
  90. appName) + ex.ToString(),   
  91. "Can't execute.",   
  92. MessageBoxButtons.OK,   
  93. MessageBoxIcon.Error);   
  94. }   
  95. #endregion   
  96. }   
  97. }   

【编辑推荐】

  1. C#中定义装箱和拆箱详解
  2. 浅谈C#类型系统
  3. 三种不同的C#异常类型
  4. 详细介绍C#编译器
  5. C#异常机制的相关解释
THE END