135-1821-9792

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

当前题目:如何在Win界面上完成C#编译
文章网址:http://www.wenjiangwz.cn/article/djipssi.html

其他资讯



Copyright © 2009-2022 www.wenjiangwz.cn 成都网站建设公司 版权所有 蜀ICP备19037934号