none
C# Issue executing command from command line RRS feed

  • Question

  • Hi,

    I can execute the following batch program from the command line by going to the folder the program resides in and typing the name of the program and pressing enter.  However when I am try to execute a batch program from C# with the following command:

    Process.Start("CMD.exe", "/K \"" + AppInfo.ProjectLocation + "\\init\\batch.exe\"");

    The batch program gives error the following error:

    2019/03/06 13:56:51 Cannot create fileopen ../../logs/error.txt: The system cannot find the path specified.

    It seems as though when I try to run this program via C# it's using the directory of C# program to try and find its files instead of it's own directory.  What could I be missing?

    Wednesday, March 6, 2019 6:59 PM

Answers

All replies

  • Try this:

    Process.Start("CMD.exe", "/K cd /D \"" + AppInfo.ProjectLocation + "\\init\" & \"" + AppInfo.ProjectLocation + "\\init\\batch.exe\"");


    • Edited by Viorel_MVP Wednesday, March 6, 2019 8:10 PM
    Wednesday, March 6, 2019 8:08 PM
  • Seems there is some exception being thrown, but the exception handler cannot write to the log file specified, hence throwing another exception.

    Just move your binary at least 2 level deeper (the error message is "The system cannot find the path specified" instead of "Access denied" so I think you're running your application at root folder or just one level lower, and this is apparently not enough), and make sure your application have permission to write on "..\.." folder for it to log what is going on.


    Thursday, March 7, 2019 1:13 AM
    Answerer
  • Hi,

    I got the following error when I tried it:

    '"C:\myprograms\TESTS\init\batch.exe"' is not recognized as an internal or external command,
    operable program or batch file.

    Thursday, March 7, 2019 2:26 AM
  • I figured out what I did wrong.  Thanks that worked.
    Friday, March 8, 2019 1:57 AM
  • Hi wegoodwin,

    Could you sharing you solution with us? It would be beneficial to community members.

    And mark the solution as answer. It would make answer searching easier in the forum.

    Best Regards,

    Wendy


    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.


    Friday, March 8, 2019 2:49 AM
    Moderator
  • Hi Wendy,

    The solution that I marked as correct worked.  I had forgotten to transpose the name of the exe when I copy and pasted the solution in my code.  

    Tuesday, March 19, 2019 1:37 AM