如何在Win界面上完成C
创始人
2024-04-29 12:30:24
0

本文只是可以让大家摆脱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#异常机制的相关解释

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...