As part of my program I need to run a batch file.
The person I'm doing it for doesn't want anyone to be able to view the batch script, so I tried to include it in a WinForms solution, but I can't get it to run, I just get a time out error.
Asked
Active
Viewed 215 times
-3
-
OK, so how are you trying to do it? Where are you storing the source? How are you invoking it? Welcome to Stack Overflow, by the way, but the best way to get an answer here is to provide a [mcve] - you show us your code and we can (hopefully) show you where your issue is. – Flydog57 Jan 06 '19 at 04:01
-
See if this one can help: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). There's also a sample project you can download for testing. Read the description of the procedure. – Jimi Jan 06 '19 at 04:15
1 Answers
2
Michael,
You can do it two different way.
To provide script file in Process, but it will expose your code, as its open in file.
You can start command prompt by your process and supply your code as an argument.
var p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c mycmd.exe 2>&1";
OR
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c dir \windows";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = false;
p.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
p.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
p.WaitForExit();

Aquil Mirza
- 31
- 3