471,319 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Execute command of OS

How can I execute a command of the operating system like "cls" from a C#
code?
Thank you
Jan 9 '06 #1
4 6573
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
Use the Process class under System.Diagnostics namespace the help files
have some examples of how to use it.

Jan 9 '06 #3
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
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.

Similar topics

2 posts views Thread by Norman Fritag | last post: by
4 posts views Thread by Chris | last post: by
6 posts views Thread by Oliver | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.