WPF C#-通过cmd获取python版本

我尝试使用python版本,因此我已经开始使用cmd和命令“ python --version”启动进程。


我已经首先尝试了这个:


     using (System.Diagnostics.Process p = new System.Diagnostics.Process())

              {

               p.StartInfo.UseShellExecute = true;

               p.StartInfo.RedirectStandardOutput = false;

               p.StartInfo.FileName = "cmd.exe";

               p.StartInfo.Arguments = "/k C:/Python36/python --version";

               p.StartInfo.CreateNoWindow = false;

               var retorno = p.Start();

              }

并打开一个cmd窗口并返回以下内容:


http://img4.mukewang.com/6083b0b50001962603430038.jpg

代替此,我需要将此结果返回到我的WPF应用程序,所以我尝试这样做:


public static string GetPythonVersion()

    {

      string command = "python --version";

      string output = null;

      using (System.Diagnostics.Process p = new System.Diagnostics.Process())

      {

        p.StartInfo.UseShellExecute = false;

        p.StartInfo.RedirectStandardOutput = true;

        p.StartInfo.FileName = "cmd.exe";

        p.StartInfo.Arguments = String.Format(@"/c {0}\{1} ", "C:/Python36/", command);

        p.StartInfo.CreateNoWindow = true;

        if (p.Start())

          output = p.StandardOutput.ReadToEnd();

      }

     return output;

   }

返回空字符串给我。但是以一个示例为例,如果我使用相同的代码将“点列表”返回到我的wpf应用程序中,效果很好,但在这种情况下,使用的是字符串返回空的版本...。



叮当猫咪
浏览 246回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP