DOS commands in C#

Posted on 2003-03-22
Medium Priority
Last Modified: 2012-08-14
Hello everyone,

Can someone pls explain how to run typical DOS commands in C# code? For example open ftp connection to some site, provide username and password, get some file and save it in, say, C:\project. Or rename, delete, move a file, just the basic things. Please be as detailed as possible...

Thanks, Nevez
Question by:nevez
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 48

Expert Comment

ID: 8189183
To run any process (including DOS commands) from C# code you need to use Process.Start method:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.Start("del", "C:\MyFile.txt");  // delete file using DOS del command

To have more control over process start operation you can use other overloaded function:

public static Process Start(
   ProcessStartInfo startInfo

To wait for the end of process use Process.WaitForExit Method or handle Exited event. When process finished use Process.ExitCode Property to get exit code.

Author Comment

ID: 8190199
The above example

p.Start("del", "C:\MyFile.txt");

doesn't really work. Error message is: "Static member 'System.Diagnostics.Process.Start(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead"

But anyway, can you pls. give an eample specifically with an ftp connection?

LVL 48

Accepted Solution

AlexFM earned 400 total points
ID: 8193387
Sorry, it was my mistake. Static function should be called by other way:

Process p = Process.Start(del", "C:\MyFile.txt");
p.WaitForExit();   // wait for process end

Replace program name and arguments in this sample with any other values that you need.
ftp command works in interactive mode, you need to use the function
Start(ProcessStartInfo startInfo)
and redefine stdin and stdout for created process, and supply answers for ftp command request. This is complicated problem, I think using .NET networking classes is the best way.

Author Comment

ID: 8196058
I'm beginning to realize the complexity of this. Thanks for your input.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question