2
string strcmd= ":START cls echo What Type of Math would you like to do?"+
"echo Add, Subtract, Multiply, or Divide." +
"set /p math=IF '%math%' == 'Add' GOTO ADD"+
"IF '%math%' == 'add' GOTO ADD"+
"IF '%math%' == 'Subtract' GOTO SUB"+
"IF '%math%' == 'subtract' GOTO SUB"+
"IF '%math%' == 'Multiply' GOTO MULTIPLY"+
"IF '%math%' == 'multiply' GOTO MULTIPLY"+
"IF '%math%' == 'Divide' GOTO DIVIDE"+
"IF '%math%' == 'divide' GOTO DIVIDE"+
"Exit";



System.Diagnostics.Process.Start("CMD.exe",strcmd);

When I run this code, Command Prompt screen appears for a sec and disappears. I want this code to be able to run in the command prompt and let the user enter a value through the command prompt. How can I do that ? (PS: I used the calculator script just as an example. I also do not want to use the .cmd file directory) I have tried this and did not really work for me.

Thanks in advance.

Community
  • 1
  • 1
JackTheRipper
  • 99
  • 1
  • 9
  • 1
    Possible duplicate of [Executing Batch File in C#](http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – raven Sep 16 '16 at 20:31
  • 1
    You are better off just saving that string to a `.bat` file (even if temporarily) and then `Process.Start()`'ing the batch file (or feed it into cmd.exe as an argument). You can use `&&` to run multiple cmd commands from a single line, but I don't think it's possible with your script that has GOTO's in it, or if there's say a `for` loop. So you could try adding `&&` at the end of each line like [this post](http://stackoverflow.com/questions/8055371/how-to-run-two-commands-in-one-line-in-windows-cmd) says. – Quantic Sep 16 '16 at 20:44
  • The problem is this piece of code is under a button in my Windows Form Application, and the directory of batch file will differ if I want to run the program on a different computer. I thought about using OpenFileDialog for user to point the .bat file. Then I thought that what is the point of user to have the application? – JackTheRipper Sep 16 '16 at 20:51
  • Well there are helper methods to get you a Windows temporary directory and/or file: https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.110).aspx. Otherwise, what is your batch file doing? Maybe the question isn't, "But then why even have my program in the fist place if they can just double click the batch file?", but should be, "Why am I using a batch file to do math functions when my c# program can do them instead?". – Quantic Sep 16 '16 at 21:08
  • Thanks for the helper methods idea. As I clarified in my question, "calculator" is used as an example. My real .cmd file creates a specific database. – JackTheRipper Sep 16 '16 at 21:14

0 Answers0