none
How to run an application and send file names? RRS feed

  • Question

  • I've donwloaded the following maps:

    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

    I need to merge the above maps. So I am using osmconvert to merge maps. I read this answer about merging maps. So if I copy the following command and paste into command window, then it works fine - it creates all.osm.pbf file:

    So desired file all.osm.pbf is created:

    However, now I would like to call this command programmatically. I mean, I would like to call the above command through C#. So I've tried this code in my Console application:

    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();
    }

    But I always see the following error:

    My files are located in D:\Downloads:

    I've tried this code, however, the error is the same:

    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";

    and:

    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";
    Could you say, please, what am I doing wrong?

    Monday, December 2, 2019 3:56 PM

Answers

  • Hi NiceStepUp,

    Thank you for posting here.

    For your question, you want to execute a exe file in C#.

    First, this command is executed by cmd.exe.

    Second, the command should be preceded by "/ c".

    Here is a code example.

              Process p = new Process();
              p.StartInfo.FileName = "cmd.exe";
              p.StartInfo.WorkingDirectory = @"d:\test";
              p.StartInfo.Arguments = "/c osmconvert64.exe andorra-latest.osm.pbf --out-o5m | osmconvert64.exe - monaco-191130.osm.pbf | osmconvert64.exe - monaco-170101.osm.pbf -o=all.osm.pbf";
              p.Start();
    

    Hope this could be helpful.

    Best Regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NiceStepUp Wednesday, December 4, 2019 6:58 PM
    Tuesday, December 3, 2019 9:56 AM

All replies

  • First off, in your second attempt you don't want to double the backslashes
    when you are using verbatim strings (preceding @).

    The error is telling you what is wrong: You are passing the pipe operator - as 
    well as the name of the exe itself - as arguments to osmconvert.exe which of 
    course they can't be.

    - Wayne

    Monday, December 2, 2019 4:53 PM
  • QayneAKing, do you mean to write like this?

    process.StartInfo.FileName = @"D:\Downloads\osmconvert.exe";

    process.StartInfo.Arguments = @"osmconvert andorra-latest.osm.pbf --out-o5m | osmconvert - azores-latest.osm.pbf | osmconvert - cyprus-latest.osm.pbf -o=all.osm.pbf"


    Monday, December 2, 2019 7:41 PM
  • Hi NiceStepUp,

    Thank you for posting here.

    For your question, you want to execute a exe file in C#.

    First, this command is executed by cmd.exe.

    Second, the command should be preceded by "/ c".

    Here is a code example.

              Process p = new Process();
              p.StartInfo.FileName = "cmd.exe";
              p.StartInfo.WorkingDirectory = @"d:\test";
              p.StartInfo.Arguments = "/c osmconvert64.exe andorra-latest.osm.pbf --out-o5m | osmconvert64.exe - monaco-191130.osm.pbf | osmconvert64.exe - monaco-170101.osm.pbf -o=all.osm.pbf";
              p.Start();
    

    Hope this could be helpful.

    Best Regards,

    Timon



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NiceStepUp Wednesday, December 4, 2019 6:58 PM
    Tuesday, December 3, 2019 9:56 AM