0Day Forums
How to execute a .bat file from a C# windows form app? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: .bat & .wsf & .cmd (https://zeroday.vip/Forum-bat-wsf-cmd)
+--- Thread: How to execute a .bat file from a C# windows form app? (/Thread-How-to-execute-a-bat-file-from-a-C-windows-form-app)



How to execute a .bat file from a C# windows form app? - Mrazoeosin433 - 07-23-2023

What I need to do is have a C# 2005 GUI app call a .bat and several VBScript files at user's request. This is just a stop-gap solution until the end of the holidays and I can write it all in C#. I can get the VBScript files to execute with no problem but I am unable to execute the .bat file. When I "click" in the C# app to execute the .bat file a DOS window opens up and closes very fast and the test .bat file does not execute - "Windows does not recognize bat as an internal or external command" is the error returned in the DOS box. If I simply doubl-click the .bat file or manually run it from the command prompt it does execute. I also need the .bat file to execute silently unless a user interaction is required - this script copies 11k+ files to folders on a networked machine and occasionally Windows "forgets" if the destination is a file or directory and asks for the user to tell it what it is (this is a whole other issue not for discussion here...needless to say I am annoyed by it).

So far in my C# source I have this :

Process scriptProc = new Process();


if (File.Exists("c:\\scripts\\batchfile1.bat"))
{

scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.Arguments = ("cmd.exe", "/C C:\\scripts\\batchfile1.bat"); // Wacky psuedo code //
scriptProc.Start();
scriptProc.WaitForExit(1500000);
scriptProc.Close();

}

if (!File.Exists("c:\\scripts\\batchfile1.bat"))
{
}

I am aware that this code does not work - but it is essentially what I want it to do. What I am looking at is something like this for .bat files. I assume I have to tell the system to use cmd to run the .bat. I am at a loss as to how to do this. I have checked out this [site][1] which is for C# 2003. Not much help for me as I am very green with C#.


EDIT: Using Kevin's post I attempted it again. Same solution script from that post but modified for me since I do not need to redirect:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "C:\\scripts\\batchfile1.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();


[1]:

[To see links please register here]




RE: How to execute a .bat file from a C# windows form app? - Prounuse722 - 07-23-2023

Here is what you are looking for:

[To see links please register here]


It's about a question as to why a service can't execute a file, but it shows all the code necessary to do so.


RE: How to execute a .bat file from a C# windows form app? - enstatite722 - 07-23-2023

For the problem you're having about the batch file asking the user if the destination is a folder or file, if you know the answer in advance, you can do as such:

If destination is a file:
echo f | [batch file path]

If folder:
echo d | [batch file path]

It will essentially just pipe the letter after "echo" to the input of the batch file.




RE: How to execute a .bat file from a C# windows form app? - hacxx - 01-28-2024

Thanks