C#进程。开始()尽管文件已存在并正在设置工作目录,但未启动

2024-04-24 10:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

Question update

我试图在Unity中使用C脚本来调用python程序。在以下两个链接之后:

How do I run a Python script from C#?

Process.Start() not starting the .exe file (works when run manually)

我无法让我的C脚本调用python程序。我知道这不是我的python脚本的问题,因为我可以在终端上毫无问题地运行程序。你知道吗

public class JsonStream : MonoBehaviour
{
    private string GetStream()
    {
        ProcessStartInfo start = new ProcessStartInfo();

        //none of the paths work, the uncommented variables return true when using File.Exists
        //while the commented variables return false
        start.FileName = "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3";
        //start.FileName = "\"/Library/Frameworks/Python.framework/Versions/3.6/bin/python3\"";

        start.Arguments = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py";
        //start.Arguments = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch/stream.py" + "\"";

        start.WorkingDirectory = Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch";
        //start.WorkingDirectory = "\"" + Directory.GetCurrentDirectory() + "/Assets/googlesheetspyprojectbatch" + "\"";

        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.ErrorDialog = true;
        start.RedirectStandardError = true;

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                return result;
            }
        }
    }

    public void DoEverythingStream()
    {
        UnityEngine.Debug.Log("Doing Stream");
        string json_text = GetStream();
        string[] games = json_text.Trim().Split('\n');
        foreach (string game in games)
        {
            UnityEngine.Debug.Log(game);
        }
    }
}

运行程序不会引起错误,但也不会输出任何内容。有人知道我的程序有什么问题吗?谢谢!你知道吗


Tags: therun程序脚本truestringreturnprocess