Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
Hello,
like the title say, I want to execute CMD command by pressing buttons in a Windows Form application.
I want to create an application which shutdown my computer after a specific time.

I´m new at Visual Studio and we only practiced a few command by creating some patterns in Forms. I also only used Forms because it`s easy to design the window where I want to press the buttons.

What I have tried:

I tried many commands, using the „using System.Diagnostics“ parameter and then the code:

ProcessStartInfo processtartinfo = new ProcessStartInfo();
processtartinfo.Arguments = "shutdown /s /t 3600";
processtartinfo.WindowStyle = ProcessWindowStyle.Hidden;
processtartinfo.FileName = "CMD.exe";
System.Diagnostics.Process.Start(processtartinfo);

But i wont work…
I also tried this code:

string strCmdText = "shutdown /s/t 3600";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

Which also didn´t work…
Posted
Updated 4-Mar-20 3:13am
v2
Comments
Richard MacCutchan 3-Mar-20 3:48am    
Change the options in ProcessStartInfo so you can see the actual running of the command, including any potential error messages.
Richard MacCutchan 4-Mar-20 9:41am    
Have you implemented either of the suggestions provided in Solution 2 below?

You're launching CMD, which if you open a CMD prompt and type "CMD /?", you'll see the the argument to launch a process is "/C" if the want the CMD window to close when the process it launched exits.

But, you don't even need to launch CMD at all. You can just launch "Shutdown.exe" and keep the Arguments as "/s /t 3600".
 
Share this answer
 
You may want to read the documentation for the Shutdown command;
1. You may need to use the /f (force) parameter with the hidden window style
2. Your time delay is invalid, maximum delay is 600 seconds.

Reference: Shutdown | Microsoft Docs[^]
 
Share this answer
 
Comments
Member 14761028 3-Mar-20 3:40am    
But when I manually execute the command in CMD or with a batchfile it works. There is no maximum delay, I also tried 3 hours (10800 sec.) which also worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900