如何从我创建的文件(.cs文件)创建一个exe文件?

时间:2011-10-05 17:18:30

标签: c# winforms

这个C#代码用于运行我已合并在一起的Winform应用程序。我想从那个C#代码创建一个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
}

}

2 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

您需要具有内置编译器的编译器或IDE

尝试使用Microsoft Visual C#Express,它是免费的