如何运行一个应用程序并发送文件名?

How to run an application and send file names?

我下载了以下地图:

andorra-latest.osm.pbf       https://download.geofabrik.de/europe/andorra-latest.osm.pbf
azores-latest.osm.pbf        https://download.geofabrik.de/europe/azores-latest.osm.pbf
cyprus-latest.osm.pbf        https://download.geofabrik.de/europe/cyprus-latest.osm.pbf

我需要合并上面的地图。所以我使用 osmconvert to merge maps. I read this answer about merging maps。因此,如果我复制以下命令并粘贴到 command window,那么它工作正常 - 它创建 all.osm.pbf 文件:

因此创建了所需的文件 all.osm.pbf

但是,现在我想以编程方式调用此命令。我的意思是,我想通过 C# 调用上面的命令。所以我在我的控制台应用程序中尝试了这段代码:

static Process process = new Process();

static void Main(string[] args)
{
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived);
    process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_ErrorDataReceived);
    process.Exited += new System.EventHandler(process_Exited);

    process.StartInfo.FileName = @"osmconvert.exe";
    process.StartInfo.Arguments = @"osmconvert.exe andorra-latest.osm.pbf --out-o5m | osmconvert.exe - azores-latest.osm.pbf | osmconvert.exe - cyprus-latest.osm.pbf -o=all.osm.pbf";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;

    process.Start();
    process.BeginErrorReadLine();
    process.BeginOutputReadLine();
}

但我总是看到以下错误:

我的文件位于 D:\Downloads:

请问我做错了什么吗?

更新:

我试过这段代码,但是,错误是一样的:

process.StartInfo.FileName = @"D:\Downloads\osmconvert.exe";
process.StartInfo.Arguments = @"D:\Downloads\osmconvert.exe D:\Downloads\andorra-latest.osm.pbf --out-o5m | D:\Downloads\osmconvert.exe - D:\Downloads\azores-latest.osm.pbf | D:\Downloads\osmconvert.exe - D:\Downloads\cyprus-latest.osm.pbf -o=D:\Downloads\all.osm.pbf";

和这种方法:

process.StartInfo.FileName = @"D:\Downloads\osmconvert.exe";
process.StartInfo.Arguments = @"D:\Downloads\osmconvert.exe D:\Downloads\andorra-latest.osm.pbf --out-o5m | D:\Downloads\osmconvert.exe - D:\Downloads\azores-latest.osm.pbf | D:\Downloads\osmconvert.exe - D:\Downloads\cyprus-latest.osm.pbf -o=D:\Downloads\all.osm.pbf";

我认为问题在于您将 osmconvert.exe 作为第一个命令行参数发送。可执行文件可能试图打开 自身 并将其作为地图数据处理。这可能会失败,因为它试图以一种在执行时不可能的方式 (read/write) 打开。

相反,您可以 "programatically" 调用 cmd.exe 并告诉它执行命令:

static Process process = new Process();

static void Main(string[] args)
{
    process.Exited += new System.EventHandler(process_Exited);

    // create a cmd.exe process.
    process.StartInfo.FileName = @"cmd.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.Start();

    var input = process.StandardInput;
    // tell cmd.exe to do your bidding.
    input.WriteLine("osmconvert.exe andorra-latest.osm.pbf --out-o5m | osmconvert.exe - azores-latest.osm.pbf | osmconvert.exe - cyprus-latest.osm.pbf -o=all.osm.pbf");
    // then tell it to exit.
    input.WriteLine("exit");
    // process.Exited event should fire at this point.
    // or you could process.WaitForExit() instead.
}