2013-09-25 62 views
1

我有此代碼的EXE製造商,但它會創建一個DLL文件:如何使運行時間

private void btnCompile_Click(object sender, System.EventArgs e) 
    { 
     CSharpCodeProvider csp = new CSharpCodeProvider(); 
     ICodeCompiler cc = csp.CreateCompiler(); 
     CompilerParameters cp = new CompilerParameters(); 

     cp.OutputAssembly = Application.StartupPath + "\\TestClass.dll"; 
     cp.ReferencedAssemblies.Add("System.dll"); 
     cp.ReferencedAssemblies.Add("System.dll"); 
     cp.ReferencedAssemblies.Add("System.Data.dll"); 
     cp.ReferencedAssemblies.Add("System.Xml.dll"); 
     cp.ReferencedAssemblies.Add("mscorlib.dll"); 
     cp.ReferencedAssemblies.Add("System.Windows.Forms.dll"); 
     cp.ReferencedAssemblies.Add("CSharpScripter.exe"); 

     cp.WarningLevel = 3; 

     cp.CompilerOptions = "/target:library /optimize"; 
     cp.GenerateExecutable = false; 
     cp.GenerateInMemory = false; 

     System.CodeDom.Compiler.TempFileCollection tfc = new TempFileCollection(Application.StartupPath, false); 
     CompilerResults cr = new CompilerResults(tfc); 

     cr = cc.CompileAssemblyFromSource(cp, this.rtfCode.Text); 

     if (cr.Errors.Count > 0) 
     { 
      foreach (CompilerError ce in cr.Errors) 
      { 
       Console.WriteLine(ce.ErrorNumber + ": " + ce.ErrorText); 
      } 
      MessageBox.Show(this, "Errors occoured", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      this.btnExecute.Enabled = false; 
     } 
     else 
     { 
      this.btnExecute.Enabled = true; 
     } 

     System.Collections.Specialized.StringCollection sc = cr.Output; 
     foreach (string s in sc) 
     { 
      Console.WriteLine(s); 
     } 
    } 

如何轉換爲EXE文件?

+1

你需要'exe'或'dll'?如果exe爲什麼你把它命名爲DLL?如果dll爲什麼你的問題標題說exe? –

回答

5
cp.GenerateExecutable = false; 

你可能want改變該行...

+0

可能還想將'/ target:library'更改爲'/ target:winexe'。見[這裏](http://msdn.microsoft.com/en-us/library/yaks5w33.aspx) – Icemanind

+0

@icemanind:或者完全擺脫它;我相信這個選擇是這個屬性設定的。 – SLaks