By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Execute command of OS

P: n/a
How can I execute a command of the operating system like "cls" from a C#
code?
Thank you
Jan 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
alberto wrote:
How can I execute a command of the operating system like "cls" from a C#
code?
Thank you

Use Process.Start("XXX.exe");
Jan 9 '06 #2

P: n/a
Use the Process class under System.Diagnostics namespace the help files
have some examples of how to use it.

Jan 9 '06 #3

P: n/a
Hi,
"alberto" <al*****@nospam.es> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
How can I execute a command of the operating system like "cls" from a C#
code?


You execute an external process using the Process class.

The example you use DO NOT WORK though, as cls is an internal command of the
command prompt, it's not an executable.
The same goes with dir , mkdir, etc.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 9 '06 #4

P: n/a
alberto wrote:
How can I execute a command of the operating system like "cls" from a C#
code?
Thank you

To execute normal program files (.exe, .com, etc.) you just use
System.Diagnostics.Process.Start("test.exe") (look in the Process class
there, more ways to do it than just .Start(programname)).

However, CLS, DIR, etc. are internal commands for the command shell and
to execute them you need to do it through the command shell executable.

For NT-based operating systems, do:

CMD.EXE /C DIR

for Win9x, do:

COMMAND.EXE /C DIR

You can probably find the right name of the program to execute by
looking in the environment variables, look for the COMSPEC variable.

Example:

System.Diagnostics.Process.Start(Environment.GetEn vironmentVariable("COMSPEC"),
@"/C DIR C:\ >C:\test.txt");
System.Diagnostics.Process.Start(@"NOTEPAD.EXE", @"C:\TEST.TXT");

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.