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

ami*_*taz -3 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
}
Run Code Online (Sandbox Code Playgroud)

}

Łuk*_*rak 6

您可以使用csc编译器并在控制台中写入此命令(csc.exe的路径可能会有所不同),我假设您的代码文件名是program.cs:

C:\Windows\Microsoft.NET\Framework64\v3.5\csc.exe /t:winexe program.cs
Run Code Online (Sandbox Code Playgroud)

可执行文件将命名为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");
}
Run Code Online (Sandbox Code Playgroud)