2011-10-05 283 views
-4

此C#代碼用於運行我合併在一起的Winform應用程序。我想從該C#代碼創建一個exe文件。如何從我創建的文件(.cs文件)創建一個exe文件?

這怎麼辦?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public static class Program 
    { 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 
} 

public class Form1 : Form 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.Text = "Form1"; 
    } 

    #endregion 
} 

}

+7

您是否嘗試過使用C#編譯器('csc.exe')? –

+0

我假設你沒有視覺工作室?從微軟獲得Visual C#Express 2010。免費! – user957902

+0

抱歉...但我知道你對我說什麼...所以我知道IDE像VS或任何東西,以及如何使用它! 我想我的C#winform生成一個exe文件!好?! ...其實...我想從我的項目(Windows窗體應用程序)生成exe文件! ...我創建一個像Visual Studio程序可以創建exe文件...但比VS更簡單! 我想要一些代碼來生成我的文件,我寫在我的第一篇文章中的exe文件! –

回答

0

你需要一個編譯器或帶有內置的編譯器的IDE

嘗試使用Microsoft Visual C#Express中,它是免費的

+0

他已經有了編譯器,他將不得不手動調用它。由於他問如何做到這一點安全地說,這不是一個選項:-) –

+0

你的回覆是不是我的問題! ...因爲我想從我的項目中的該文件生成exe文件! –

6

您可以使用CSC編譯器和控制檯寫(路徑csc.exe可能會有所不同),我假設您的代碼文件名是program.cs:

C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe /t:winexe program.cs 

可執行文件將被命名的Program.exe

如果你想從C#代碼做到這一點(如果我理解正確,你想做的事),你能做到這樣:

使用以下代碼構建並執行C#控制檯應用程序:

using System.Diagnostics; 

static void Main(string[] args) 
{ 
    Process.Start(@"C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe", "/t:winexe program.cs"); 
} 
+0

thanx ...如何使用這個使用C#代碼?! –

+0

我不知道你想要在哪裏使用這個C#代碼,我寫了一些代碼並更新了我的文章,也許這會對你有幫助。 –

+0

對不起,我沒有得到任何可執行文件。什麼是生成的.exe路徑? – SGG