代码之家  ›  专栏  ›  技术社区  ›  Sajal Dutta

在命令提示符下编译/执行C#源文件

  •  120
  • Sajal Dutta  · 技术社区  · 16 年前

    如何从命令提示符窗口编译和执行.cs文件?

    10 回复  |  直到 11 年前
        1
  •  160
  •   Two    5 年前

    CSC.exe是.NET Framework中包含的CSharp编译器,可用于从命令提示符进行编译。如果使用“/target:exe”,则输出可以是可执行文件“.exe”或DLL;如果使用/target:library,则可以在.NET Framework目录中找到CSC.exe,

    e、 g.对于.NET 3.5, c:\windows\Microsoft.NET\Framework\v3.5\ .

    要运行,首先打开命令提示符,单击“开始”,然后键入 cmd.exe
    然后,您可能需要将cd刻录到保存源文件的目录中。

      c:\windows\Microsoft.NET\Framework\v3.5\bin\csc.exe 
                /t:exe /out:MyApplication.exe MyApplication.cs  ...
    

    如果要编译多个源模块,可以将其放在同一命令行上。如果要引用其他程序集,请使用 /r:AssemblyName.dll .

    确保在一个类中定义了一个静态Main()方法,作为“入口点”。

    MyApplication ,然后是 <ENTER> 使用命令提示符。

    This article

    如果已安装Visual Studio,则在“开始菜单”中;在VisualStudioTools下,可以打开“VisualStudio命令提示符”,该提示符将设置命令行编译所需的所有环境和路径变量。

    虽然知道这一点非常方便,但您应该将其与某种构建工具的知识结合起来,例如 NAnt MSBuild , FinalBuilder

    在Mac上

    csc

    $ csc /target:exe /out:MyApplication.exe MyApplication.cs ...
    

    然后运行它:

    $ mono MyApplication.exe
    
        2
  •  20
  •   Aditya Garg    10 年前

    (未使用Visual Studio或未安装Visual Studio) 是在环境变量中创建一个用户变量,即“PATH”。

    复制此变量中的以下路径:

    “C:\Windows\Microsoft.NET\Framework\v4.0.30319”

    或者取决于您的电脑使用的.NET。

    简单使用

    “C:\Users\UserName\Desktop>csc[options]filename.cs”

        3
  •  16
  •   Konrad Krakowiak Ajay Katwe    9 年前

    您可以编译一个C#程序:

    c:>csc Hello.cs

    你可以运行这个程序

        4
  •  11
  •   DeepSpace101    8 年前

    对于最新版本,首先打开Powershell窗口,转到任何文件夹(例如。 c:\projects\ )并运行以下命令

    # Get nuget.exe command line
    wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile nuget.exe
    
    # Download the C# Roslyn compiler (just a few megs, no need to 'install')
    .\nuget.exe install Microsoft.Net.Compilers
    
    # Compiler, meet code
    .\Microsoft.Net.Compilers.1.3.2\tools\csc.exe .\HelloWorld.cs
    
    # Run it
    .\HelloWorld.exe    
    

    示例HelloWorld.cs

    using System;
    
    public class HelloWorld {
        public static void Main() 
        {
            Console.WriteLine("Hello world!");
        }
    }
    

    .\Microsoft.Net.Compilers.1.3.2\tools\csi.exe
    > Console.WriteLine("Hello world!");
    Hello world!
    
        5
  •  8
  •   Marc Gravell    16 年前

    express edition 是免费的,而且很划算-P

    snippy

    最后,请注意命令行是特定于实现的;对于微软来说,这是一个挑战 csc ; 对于 mono 是的 gmcs 还有朋友。。。。同样,要执行:对于MS版本,它只是“exename”,但是对于mono,它通常是“mono exename”。

    MSBuild , NAnt

        6
  •  6
  •   Vadzim    7 年前

    Is it possible to install a C# compiler without Visual Studio?

    那就跑吧

    "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\Roslyn\csc.exe" MyApplication.cs

    将单个源文件编译为可执行文件。

    doesn't support compiling single source file 没有预先配置的项目。

        7
  •  5
  •   swille    11 年前

    LinqPad是测试一些C#代码的快速方法,而且是免费的。

        8
  •  2
  •   Cerebrus    16 年前

    您可以在VS命令提示符下生成类文件(以便加载所有必需的环境变量),而不是默认的Windows命令窗口。

    this article .

        9
  •  2
  •   Buddhika Chathuranga    5 年前

    csc 命令并在开发人员命令提示符内使用应用程序的名称运行应用程序。

    您可以打开开发人员命令提示符,如下所示。

    希望这有帮助!

        10
  •  2
  •   Rafiul Islam    5 年前

    添加到路径

    C:\Windows\Microsoft.NET\Framework\v3.5
    

    csc file.cs
    

    执行:

    file
    
        11
  •  1
  •   gravity tata    8 年前

    一旦你写了c代码并保存它。您可以像其他代码一样使用命令提示符来执行它。

    汇编:

    mcs yourfilename.cs
    

    执行:

    mono yourfilename.exe 
    

    如果希望.exe文件与其他文件名不同,请键入

    汇编:

    mcs yourfilename.cs -out:anyname.exe 
    

    mono anyname.exe 
    

    这应该有帮助!

        12
  •  1
  •   Mr Lister hawi    6 年前

    在Windows系统中,使用命令 csc <filname>.cs

    有两种方法:

    使用命令提示符:

    1. 开始-->命令提示符
    2. 使用命令:csc/.cs

    使用开发人员命令提示符:

    1. (此处目录已设置为Visual Studio文件夹)

    希望有帮助!

        13
  •  0
  •   dbc    5 年前

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn

    在这里可以找到支持c#7的c#编译器,否则它将使用只支持c#5的.net 4编译器

        14
  •  0
  •   Anuga    5 年前

    微软已经将其编译器迁移到Github(当然):

    https://github.com/dotnet/roslyn

        15
  •  0
  •   Matt    5 年前

    我已经写了一个批处理文件 CompileCS.cmd the batch program 'CompileCS' here .

    它的 用法

    CompileCS /run GetDotNetVersion.cs [arg1] ... [arg9]

    /run

    控制台应用程序 GetDotNetVersion.cs this answer .

        16
  •  0
  •   Alex from Jitbit    3 年前

    PowerShell可以执行开箱即用的C#代码。

    要编译的一行程序&执行一个文件:

    (Add-Type -Path "Program.cs" -PassThru)::Main() #'Main' is the entry point
    

    using System;
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Hello from C#");
        }
    }
    
        17
  •  0
  •   Suryadeepto Roychowdhury    3 年前
    dotnet
    

    这是Ooold。但由于这是一个初学者,当你问问题时,如何理解如何使用控制台中的C类或C++来使用没有VisualStudio CE的编译器(如果您还没有使用它,则强烈建议使用C.B.BTW),那么最终会在.NETFramework和库和SDKS的LINO中变得更加混乱。

    1. 理解框架和SDK之间的区别。

    了解什么是C#和什么是.NET。

    3. https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro

    一路上你会把剩下的捡起来。 但我建议不要使用框架,而是使用SDK,主要是坚持VS。对我来说,我学习了C#for unity,主要是游戏开发。

    而且 谷歌 他是你的朋友。提出问题,保持好奇心。