【问题标题】:The specified executable is not a valid application for this OS platform.'指定的可执行文件不是此 OS 平台的有效应用程序。
【发布时间】:2019-08-25 01:10:22
【问题描述】:

当我尝试运行此代码时,我不断收到错误 System.ComponentModel.Win32Exception: The specified executable is not a valid application for this OS platform.

似乎myProcess.Start() 行是问题所在。 谁能帮帮我。

        string python = @"C:\Users\mk\Desktop\A.py";

        // python app to call 
        string myPythonApp = "sum.py";

        // dummy parameters to send Python script 
        int x = 2;
        int y = 5;

        // Create new process start info 
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);

        // make sure we can read the output from stdout 
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;

        // start python app with 3 arguments  
        // 1st arguments is pointer to itself,  
        // 2nd and 3rd are actual arguments we want to send 
        myProcessStartInfo.Arguments = myPythonApp + " " + x + " " + y;

        Process myProcess = new Process();
        // assign start information to the process 
        myProcess.StartInfo = myProcessStartInfo;

        Console.WriteLine("Calling Python script with arguments {0} and {1}", x, y);

        // start the process 
        myProcess.Start();

    }
}

}

【问题讨论】:

    标签: c# python visual-studio


    【解决方案1】:

    您正试图将A.py 作为可执行文件调用,例如.exe,但它不是(它只是文本,而不是程序集)。

    相反,您可以通过 CMD 调用 Python 来运行 A.py,并将 sum.py 作为额外参数传入。

    这是一个例子:

    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe"); 
    
    // run python from console.
    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardOutput = true;
    myProcessStartInfo.Arguments = "/C python A.py " + myPythonApp + " " + x + " " + y; 
    
    Process myProcess = new Process(); 
    myProcess.StartInfo = myProcessStartInfo;
    // start the process 
    myProcess.Start();
    

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-09
      • 2016-10-11
      • 2018-03-30
      • 1970-01-01
      相关资源
      最近更新 更多