【问题标题】:How to run .exe from C#如何从 C# 运行 .exe
【发布时间】:2012-09-28 09:11:13
【问题描述】:

我正在用 C# 开发 wpf 应用程序。我在我的应用程序中使用 grib 文件。我想将此 grib 文件内容转换为 csv 文件。从命令提示符我可以很容易地做到这一点。为此,我需要在命令提示符下转到 degrib.exe 的文件夹位置,即 c:\ndfd\degrib\bin。对于任何其他路径命令将不会被执行。我正在使用以下命令

C:\ndfd\degrib\bin\degrib D:\Documents\Pacificwind.grb -C -msg 1 -Csv
C:\ndfd\degrib\bin\degrib D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv

命令成功执行。我可以在 C:\ndfd\degrib\bin 文件夹中看到生成的 csv 文件。我应该如何从 C# 执行相同的命令。我见过不同的例子,但没有一个对我有用。您能否提供我可以解决上述问题的任何代码或链接?

【问题讨论】:

  • 你确定他们没有工作吗?或者文件不在您预期的位置
  • 你能把不起作用的代码贴出来吗?
  • 我很想在 .Net 中编写一个 Grib 解析器。 CSV 不是用作中介的特别理想的格式。 wmo.int/pages/prog/www/WDM/Guides/Guide-binary-2.html
  • 或者更好的是,重复使用这个sourceforge.net/projects/gribcs
  • BugFinder 你是对的。他们在工作。我在看错误的位置。非常感谢 BugFinder。

标签: c# wpf exe process.start


【解决方案1】:

这将起作用,除非您提供的路径不正确:

Process.Start(@"C:\ndfd\degrib\bin\degrib", 
              @"D:\Documents\Pacificwind.grb -C -msg 1 -Csv");

Process.Start(@"C:\ndfd\degrib\bin\degrib", 
              @"D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv")

【讨论】:

    【解决方案2】:

    您可以使用ProcessStartInfo 类为启动的应用程序设置工作目录。
    例如

            ProcessStartInfo pInfo = new ProcessStartInfo("degrib.exe");
            pInfo.WorkingDirectory = @"C:\ndfd\degrib\bin" 
            pInfo.Arguments = @"D:\Documents\Pacificwind.grb -C -msg 1 -Csv";    
            Process p = Process.Start(pInfo);
    
            // Are I assume that the second processing need to wait for the first to finish
            p.WaitForExit();
    
            // Start the second run.....
            pInfo = new ProcessStartInfo("degrib.exe");
            pInfo.WorkingDirectory = @"C:\ndfd\degrib\bin" 
            pInfo.Arguments = @"D:\Documents\Pacificwind.grb -C -msg all -nMet -Csv";    
            Process.Start(pInfo);
    

    还要查看Process 类和 WaitForExit 方法的文档

    编辑:我真的不知道它是什么degrib,现在我已经更新了对你想要得到的合理假设的答案。请让我知道路径和可执行文件名称是否正确。

    【讨论】:

      【解决方案3】:

      你可以使用下面的方法来执行你的exe文件

      System.Diagnostics.Process.Start(exePath + "LSAPP.exe");
      

      【讨论】:

      • 永远不要将路径与+结合起来。
      • 使用System.IO.Path.Combine() 组合路径。
      【解决方案4】:
      using (Process process = Process.Start(...))
          process.WaitForExit(); // You can wait for process to exit or go idle.
      

      【讨论】:

        【解决方案5】:

        您可以像这样使用Process.Start() 创建一个 Process 对象 进程 = 新进程 { StartInfo = startInfo }; 并创建您的 ProcessStartInfo 对象

        startInfo = new ProcessStartInfo(pathToExecutable, arguments);
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = false;
        startInfo.RedirectStandardOutput = true;
        process = new Process { StartInfo = startInfo };
        

        并使用 process.OutputDataReceived event

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-22
          • 2014-04-02
          • 1970-01-01
          • 2017-08-11
          • 2012-03-29
          • 2011-07-15
          • 2018-06-14
          相关资源
          最近更新 更多