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

开发 后端
本文向您介绍如何在Win界面上完成C#编译,通过建立一个CSharpCodeProvider并提供CompilerParameters等参数可以轻松实现。

本文只是可以让大家摆脱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#异常机制的相关解释
责任编辑:冰荷 来源: it55
相关推荐

2020-12-31 07:31:10

C# 反射数据

2011-08-04 15:55:50

Windows 编译 Objective-

2021-03-07 16:37:52

C#应用程序

2021-02-01 12:36:59

C# Channels存储

2009-08-18 11:17:37

C#添加鼠标右键

2023-07-30 22:25:00

JavaScrip服务端Web

2021-01-18 05:18:18

C# 8模式C# 7

2021-01-19 05:30:55

C# 8异步流IEnumerable

2022-05-18 07:09:35

C#语言架构

2009-11-02 17:15:42

C#转换为VB.NET

2021-01-22 05:53:08

C# IndexRange

2021-01-28 05:14:40

C#接口签名

2013-01-14 15:29:32

用户界面UI设计光环效应

2009-08-14 00:30:09

C#条件编译指令

2009-08-31 18:24:26

编译C#文件

2009-08-27 16:29:18

C#动态编译

2009-08-10 17:12:54

C#编译器

2009-08-13 17:36:54

编译C#代码

2009-08-07 17:32:17

C#编译程序

2018-07-26 08:36:35

Azure Funct编程Chef
点赞
收藏

51CTO技术栈公众号