简体   繁体   English

使用C#运行Python脚本的行为很奇怪

[英]Running Python script with C# acts strange

This question have been asked and answered before (for example here: How do I run a Python script from C#? ), but in my specific code the answers doesn't seem to work for me. 之前已经有人问过这个问题(例如,在这里: 如何从C#运行Python脚本? ),但是在我的特定代码中,答案似乎对我不起作用。

Github issue: https://github.com/nopara73/JoinMarketTest/issues/2 Github问题: https : //github.com/nopara73/JoinMarketTest/issues/2

In cmd it seamlessly works: 在cmd中,它可以无缝工作:

"python wallet-tool.py generate" “ python wallet-tool.py生成”

This command generates a new wallet. 此命令生成一个新的钱包。 It asks for password, password confirmation and wallet name from the user. 它要求用户提供密码,密码确认和钱包名称。

However I doesn't seem to get it right with C# code. 但是,我似乎无法用C#代码来解决问题。

At first glance it seems like an easy one, but something unusual is happening here. 乍一看,这似乎很容易,但是这里却发生了不寻常的事情。

The simpified code: 简化代码:

internal string Generate()
{
    ProcessStartInfo start = new ProcessStartInfo
            {
                FileName = PythonPath,
                Arguments = "wallet-tool.py generate",
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardInput = true
            };
    using (var process = Process.Start(start))
            {
                if (process == null) return String.Empty;
                using (var output = process.StandardOutput)
                {
                    using (var input = process.StandardInput)
                    {
                        // Asks for password.
                        var result = output.ReadToEnd();
                        input.WriteLine("password");
                        // Asks for password confirmation.
                        result += output.ReadToEnd();
                        input.WriteLine("password");
                        // Asks for wallet file name.
                        result += output.ReadToEnd();
                        input.WriteLine("wallet.json");
                        result += output.ReadToEnd();
                        return result;
                    }
                }
            }
}

作为替代方案,我已更改为IronPython。

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

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