简体   繁体   English

从Linux上的命令行启动C#程序

[英]Start C# program from command line on Linux

What command should I use to start this C# program from the command line in Linux? 我应该用什么命令从Linux命令行启动这个C#程序? I've already compiled it (using Monodevelop), but I don't know how to start it from the command line. 我已经编译了它(使用Monodevelop),但我不知道如何从命令行启动它。

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

The command line you need to start a C# (or any other .NET) program on Linux depends on how you have your Linux system configured. 在Linux上启动C#(或任何其他.NET)程序所需的命令行取决于您如何配置Linux系统。

The standard answer is to run the mono program, and pass the name of your executable assembly as a parameter. 标准答案是运行mono程序,并将可执行程序集的名称作为参数传递。 The name of your executable assembly is typically the same as the name of your project file, though you can easily change it; 可执行程序集的名称通常与项目文件的名称相同,但您可以轻松更改它; just look for a file ending in .exe after you're done compiling. 在完成编译后,只需查找以.exe结尾的文件。 It will be found in a folder named bin\\Debug , or bin\\Release or something similar (it depends on how you have your project build settings set up). 它将在名为bin\\Debugbin\\Release或类似的文件夹中找到(它取决于您如何设置项目构建设置)。 So, if you built a program called hello.exe you would go into your project folder and run: 因此,如果您构建了一个名为hello.exe的程序,您将进入项目文件夹并运行:

~/projects/hello $ mono bin\Debug\hello.exe

The reason you need to run the mono program is because Linux does not know, by default, how to run the .NET runtime automatically. 您需要运行mono程序的原因是因为Linux默认情况下不知道如何自动运行.NET运行时。 When you install .NET on Windows, it actually changes the part of the OS that loads programs, so Windows just automatically recognizes a .NET program and loads the runtime. 当您在Windows上安装.NET时,它实际上会更改加载程序的操作系统部分,因此Windows只会自动识别.NET程序并加载运行时。 On Linux, you need to do that yourself, by running the mono program first. 在Linux上,您需要首先运行mono程序来自己完成。

If you run a lot of managed code on Linux, you can also configure the Linux kernel to work the same way that Windows does. 如果在Linux上运行大量托管代码,还可以将Linux内核配置为与Windows相同的方式。 Linux has support for "miscellaneous binary formats" that allows you to tell Linux how to execute binaries that are not native Linux format. Linux支持“其他二进制格式”,允许您告诉Linux如何执行非本机Linux格式的二进制文件。 This is somewhat advanced - it likely requires you to build a custom kernel, though I would not be surprised if your Linux distribution had a better way to do it. 这有点高级 - 它可能需要你构建一个自定义内核,但如果你的Linux发行版有更好的方法,我也不会感到惊讶。 More information on this process can be found here: 有关此过程的更多信息,请访问:

http://www.kernel.org/doc/Documentation/mono.txt http://www.kernel.org/doc/Documentation/mono.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM