【问题标题】:How to run an application and send file names?如何运行应用程序并发送文件名?
【发布时间】:2020-03-27 05:12:46
【问题描述】:

我已经下载了以下地图:

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 来合并地图。我读了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";

【问题讨论】:

  • 你到底想做什么?启动三个文件的进程?将输出通过管道传输到另一个可执行文件?因为管道在这里没有任何意义。如果你只是想用三个文件启动可执行文件,那么一个接一个地启动它们
  • 尝试将完整或相对路径传递给您的地图,而不仅仅是文件名。
  • 你需要把所有文件和文件夹的完整路径名。 Process 类以空环境 PATH 开头,因此找不到可执行文件。
  • @scai 感谢您的回复。怎么给相对地图?
  • @Learner 尝试传递 D:\\Downloads\\azores-latest.osm.pbf 而不是 azores-latest.osm.pbf

标签: c# openstreetmap


【解决方案1】:

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

相反,您可以“以编程方式”调用 c​​md.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.
}

【讨论】:

    猜你喜欢
    • 2014-12-17
    • 2022-01-02
    • 2017-12-18
    • 2012-11-09
    • 2014-08-24
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多