Calling Batch File From C#

后端 未结 2 1032
被撕碎了的回忆
被撕碎了的回忆 2021-02-20 10:36

I am hoping that this is an easy question, but i have the following code in my C# application and for some reason it will not execute the batch file I am pointing to.

         


        
相关标签:
2条回答
  • 2021-02-20 11:16

    Try setting the proc.StartInfo.UseShellExecute to true; this tells the OS to perform a lookup of the file extension to find the correct handler in the registry.

    0 讨论(0)
  • 2021-02-20 11:29

    This should work

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = "C:\\Watcher\\Cleanup.bat";
    proc.StartInfo.WorkingDirectory = "C:\\Watcher";
    proc.Start();
    

    You need to set the WorkingDirectory otherwise the command will be executed in what is the current directory of the calling application

    0 讨论(0)
提交回复
热议问题