自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在Win界面上完成C#編譯

開發(fā) 后端
本文向您介紹如何在Win界面上完成C#編譯,通過建立一個(gè)CSharpCodeProvider并提供CompilerParameters等參數(shù)可以輕松實(shí)現(xiàn)。

本文只是可以讓大家擺脫csc的約束,在Win界面上完成C#編譯編譯.

在C#編譯過程中你必須以下面的步驟完成:

1.建立一個(gè)CSharpCodeProvider 實(shí)例(如果是使用Visual Basic則使用VBCodeProvider)

2.包含接口ICodeCompiler

3.提供CompilerParameters的參數(shù)

4.使用CompileAssemblyFromSource方法編譯。

5.運(yùn)行CompilerResults

6.執(zhí)行C#編譯好的程序

編譯的代碼可以是寫在文本框中的字符串,當(dāng)然也可以源文件。

  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#類型系統(tǒng)
  3. 三種不同的C#異常類型
  4. 詳細(xì)介紹C#編譯器
  5. C#異常機(jī)制的相關(guān)解釋
責(zé)任編輯:冰荷 來源: it55
相關(guān)推薦

2020-12-31 07:31:10

C# 反射數(shù)據(jù)

2021-03-07 16:37:52

C#應(yīng)用程序

2011-08-04 15:55:50

Windows 編譯 Objective-

2021-02-01 12:36:59

C# Channels存儲(chǔ)

2009-08-18 11:17:37

C#添加鼠標(biāo)右鍵

2021-01-18 05:18:18

C# 8模式C# 7

2021-01-19 05:30:55

C# 8異步流IEnumerable

2009-11-02 17:15:42

C#轉(zhuǎn)換為VB.NET

2022-05-18 07:09:35

C#語言架構(gòu)

2009-08-14 00:30:09

C#條件編譯指令

2009-08-31 18:24:26

編譯C#文件

2009-08-13 17:36:54

編譯C#代碼

2009-08-27 16:29:18

C#動(dòng)態(tài)編譯

2009-08-07 17:32:17

C#編譯程序

2009-08-10 17:12:54

C#編譯器

2023-07-30 22:25:00

JavaScrip服務(wù)端Web

2021-01-22 05:53:08

C# IndexRange

2021-01-28 05:14:40

C#接口簽名

2018-07-26 08:36:35

Azure Funct編程Chef

2009-08-04 10:29:06

在C#中使用存儲(chǔ)過程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號