2011-10-05 234 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