Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On button click in a winform application I am executing cmd.exe file (Using system("cmd.exe");), which opens perfectly fine and displays the following:

  Microsoft Windows XP [Version 6.1.7601]
  (C) Copyright 1985-2001 Microsoft Corp.

  C: A\Documents\Visual Studio 2010\Projects\WinformTest\WinformTest> 

What I want is when a user click the button, the cmd.exe open with default command and a message like this:

 Please change command options and press enter to get started experimenting   
 C: A\Documents\Visual Studio 2010\Projects\WinformTest\WinformTest> reduction -x 33554432 -i  

Notice a new command and a message is already there.

Can anybody tell me how to go for this?

UPDATE

 cmd /k echo Please change command options and press enter to get started      
 experimenting & echo reduction -x 33554432 -i

This shows closest solution, but here I have to set the commands every time I open the cmd. Is it possible to set the command once and save the setting, so next time I open CMD the message is displayed? Or it is possible to pass these commands options/switches when I am opening it in my application using system("cmd.exe") so that at run time this setting for message is done?

Solved

 system("cmd.exe /k echo Please change command options and press enter to get started  
 experimenting & echo reduction -x 33554432 -i"  );

But this echoes maeesage but the command is not comming at the prompt.

share|improve this question

2 Answers 2

One way to do this is to make use of the little known autorun feature of the command processor. I remembered that this was possible so did a Google search only to be given a link to my own Blog! I'd forgotten about writing this:

http://it.knightnet.org.uk/2009/05/how-to-show-a-message-of-the-day-motd-at-the-windows-command-prompt.html

You can set an autorun command via the registry at either of these locations according to need:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor

I use the following setting:

%USERPROFILE%\.profile.cmd

Which runs that cmd file every time I start a command line. In the autorun command you could simply display the output of a text file. In your app, dump some text to the text file, start the command line. You should then empty the text file (don't delete it) so you don't keep getting the prompt.

You could add some further intelligence if needed so that the text file is only displayed for your app.

Of course, you could also change the registry on the fly from your app.

share|improve this answer

cmd.exe accepts an optional /k parameter, which you could use to have it execute a command when it starts up. To use this to get something close to what you want, something like the following seems close:

cmd /k echo Please change command options and press enter to get started experimenting & echo reduction -x 33554432 -i

which results in a window opening and displaying:

enter image description here

As you can see, you can execute multiple commands by putting an "&" between each.

If what you want to do something more complex you can have it execute a separate .cmd/.bat script to achieve something similar to what Julian Knight talks about in his answer, but without requiring registry modifications.

share|improve this answer
    
But here I have to set the commands every time I open the cmd. Is it possible to set the command once and save the setting, so next time I open CMD the message is displayed? Or it is possible to pass these commands switches when I am opening it in my application using system("cmd.exe") so that at run time the setting for message is done? –  gpuguy Jun 28 '12 at 2:11
    
This worked: system("cmd.exe /k echo Please change command options and press enter to get started experimenting & echo reduction -x 33554432 -i" ); –  gpuguy Jun 28 '12 at 2:34
    
@gpuguy: Your comment that it worked was what I was suggesting you do in my answer -- sorry, because apparently that wasn't clear enough. –  martineau Jun 28 '12 at 2:57
    
sorry for any confusion –  gpuguy Jun 28 '12 at 4:00
    
@qpuguy: You might be able to use SendKeys() to preload cmd's keyboard buffer. –  martineau Jun 28 '12 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.