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 last published in July 2004

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.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close