Q

How do I run a DOS command in .NET and place the results in a text file with specified path?

Here we demonstrate how to run a DOS command in VB .NET and place the results in a text file with the appropriate path specified.

How do I run DOS a command in .NET and place the results in a text file with specified path? (e.g. DIR)
You have to use the System.Diagnostics.Process class. The following code issues a dir command and retrieves all the output generated by the command prompt in a string variable. You can write its contents to a file if you want:
ProcessStartInfo si = new ProcessStartInfo("cmd.exe");
// Redirect both streams so we can write/read them.
si.RedirectStandardInput = true;
si.RedirectStandardOutput = true;
si.UseShellExecute = false;
// Start the procses.
Process p = Process.Start(si);
// Issue the dir command.
p.StandardInput.WriteLine(@"dir c:");
// Exit the application.
p.StandardInput.WriteLine(@"exit");
// Read all the output generated from it.
string output = p.StandardOutput.ReadToEnd();

This was first published in July 2004

Dig deeper on VB 6 to VB .NET Migration

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close