Jump to content

I'm not a programmer. Let's just get that out of the way. I love the concept of the creative freedom programming gives people so I've been trying to teach myself it just a little bit.

 

I'm trying to write a basic CLI program that interacts with the command prompt to alter Windows settings (It will make it so I don't have to go diving all throughout the OS to change all the settings I want on a fresh install). Using Microsoft Visual Studio I've concluded when running the program with no logic (or my own input code) it isn't actually the command prompt with the usable commands. It just looks like the command prompt. There's no logic behind it to make it do anything.

 

I should be able to create my own commands to make it respond to specific inputs however when I want it to execute legitimate CMD commands to influence the system I run into an issue. Now research has shown me what seems to be the most popular method for running a command which is:

System.Diagnostics.Process.Start("CMD.exe","input command or variable here ex. /C or /K [ipconfig /all] or any other command");

or
String viewIpConfig = "ipconfig /all";
System.Diagnostics.Process.Start("CMD.exe",viewIpConfig);

or 
String viewIpConfig;
viewIpConfig = "ipconfig /all";
System.Diagnostics.Process.Start("CMD.exe",viewIpConfig);

In any of these instances (in W10) the CMD window that would open would then close immediately but that's a different issue that I'll research an answer for later.

 

My current issue is that certain processes I want to run in CMD require the execution of multiple lines sequentially. The above code doesn't seem to allow this and if I were to copy/paste multiple commands like above and just stack them in the C# code each execution would open a new CMD.exe instance and run it in its own window. This won't work for certain things I want to do. Is there a way in C# to execute multiple lines sequentially in the same CMD.exe window?

 

There's a horrible workaround which is to have multiple of these lines write commands to a .bat document then have the final command execute the .bat and the last command within the file delete the file but for every line written to the file would be a separate CMD.exe window and although it closes itself this just sounds like a horrible practice because that could be 10's of separate CMD.exe task executions zipping by before the last one executes the batch file and does what I actually wanted it to do.

 

TL;DR

How might I run multiple CMD commands sequentially from the same window using C#?

Link to comment
https://linustechtips.com/topic/907406-help-with-writing-basic-console-app-in-c/
Share on other sites

Link to post
Share on other sites

If you're making something to set up your fresh Windows install, a PowerShell or Batch script might actually be better than writing a console app. I would highly recommend PowerShell if you're fiddling with Windows settings.

 

You're also being quite vague on what you actually want to accomplish, which is a bit confusing for me to write a proper recommendation.

Link to post
Share on other sites

Just now, HarryNyquist said:

If you're making something to set up your fresh Windows install, a PowerShell or Batch script might actually be better than writing a console app. I would highly recommend PowerShell if you're fiddling with Windows settings.

 

You're also being quite vague on what you actually want to accomplish, which is a bit confusing for me to write a proper recommendation.

I just like the idea of a console app because of it's higher capability. I won't be limited to just what CMD.exe can do but have an entire language to do a near infinite number of other things. Though in the past I have written garbage applications comprised strictly of batch files and a lot of goto statements. I just don't like it, I don't like having to use batch files. They're too easy to manipulate and edit. I want to try something different.

 

To be more specific. Things such as emptying all the useless squares from inside the start button, removing the useless shortcuts in the task bar. Disabling Cortana in the Group Policy Editor. Disabling pretty much everything under the Privacy section under Settings. Numerous other things.

 

At the same time I'm not limiting this to a one press button for a fresh windows install. I also want to learn C# in general and I love playing with the command prompt which would let me incorporate both of them into a console application or windows form app to complete tasks or help me with something I'm doing. If I get good enough at it I can use it to help others.

 

Hopefully this is a sufficient explanation for my reasoning to want to know these things.

 

My backup idea is to find a way for c# to write the batch file directly. Then execute it. Then have the last CMD command delete the file.

Link to post
Share on other sites

ProcessStartInfo can handle more parameter settings that can be useful.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /all";

process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

Added '/C' will exit command window when done and process.WaitForExit() will wait for process to end.

 

Have a look here for more options with ProcessStartInfo.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×