【问题标题】:How to run C# Console Applications via Python?如何通过 Python 运行 C# 控制台应用程序?
【发布时间】:2021-08-19 09:47:13
【问题描述】:

我使用 Visual Studio 创建了以下 C# 控制台应用程序 (.NET Core 3.1)

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //Check if args contains anything
            if (args.Length > 0)
            {
                Console.WriteLine("args = " + args[0]);
            } else
            {
                Console.WriteLine("args is empty");
            }

            //Prevent the application from closing itself without user input
            string waiter = Console.ReadLine();
        }
    }
}

我可以通过 cmd.exe 使用一个参数成功执行应用程序: CMD Input and Output

现在我想通过 Python 使用一个参数来运行这个程序。我已阅读 How to execute a program or call a system command? 并尝试实现它。但是,应用程序似乎没有正常运行。

我通过 Jupyter notebook 使用的 python 代码:

import subprocess
path = "C:/Users/duykh/source/repos/ConsoleApp2/ConsoleApp2/bin/Release/netcoreapp3.1/ConsoleApp2.exe"
subprocess.Popen(path) //#Also tried with .call and .run
                        //#subprocess.Popen([path, "argumentexample"]) doesn't work either

输出:

<subprocess.Popen at 0x2bcaeba49a0>

我的问题是:

为什么它没有(正确)运行,我如何用一个参数正确运行这个应用程序?

【问题讨论】:

  • 也许另一种选择是从 Python 调用/运行 shell 或 cmd 并从那里运行应用程序?

标签: python c# console-application


【解决方案1】:

我已经回答了我自己的问题。简而言之:我很愚蠢。

  1. Jupyter Notebook 在后台运行,应用程序正在那里运行。这就是它没有打开新提示的原因。

  2. subprocess.Popen([path, "argument example"]) 似乎可以很好地运行带有参数作为输入的控制台应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2013-02-11
    • 2023-03-19
    • 1970-01-01
    • 2013-06-30
    • 2010-10-16
    • 2013-06-10
    相关资源
    最近更新 更多