Command results to a file

  • Thread starter Thread starter Nancy
  • Start date Start date
N

Nancy

At run comand line, we could type DIR or NET VIEW ETC...
and the results are displayed on the screen.
How could I automatically save those results into a text
file?
I tried to do the following:

Dim StreamWriter As StreamWriter
StreamWriter = New StreamWriter(strFN)
StreamWriter.Write(Shell("NET VIEW",
AppWinStyle.NormalFocus, True))
AppWinStyle.NormalFocus, True, 1))
StreamWriter.Close()
NET VIEW is displayed but not saved into the file.
Open for suggestions!
Thanks, Nancy.
 
Use same approach you would use when doing this in command window: use
redirection. You can create command which redirects output into file in
defined by you location and execute it in same way as you do interactive
variant.
HTH
Alex
 
Nancy said:
At run comand line, we could type DIR or NET VIEW ETC...
and the results are displayed on the screen.
How could I automatically save those results into a text
file?
I tried to do the following:

Dim StreamWriter As StreamWriter
StreamWriter = New StreamWriter(strFN)
StreamWriter.Write(Shell("NET VIEW",
AppWinStyle.NormalFocus, True))
AppWinStyle.NormalFocus, True, 1))
StreamWriter.Close()
NET VIEW is displayed but not saved into the file.
Open for suggestions!
Thanks, Nancy.

The best way is to use the Process class and RedirectOutput and
UseShellExecute. Then read from the redirected stream.

Dilton
 
Here's a way. Error handling, etc. issues not shown.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "NET";
psi.Arguments = "VIEW";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false; // cannot redirect if using shell execute
Process p = new Process();
p.StartInfo = psi;
p.Start();
string s = p.StandardOutput.ReadToEnd();
// do something with the captured output....
Console.WriteLine("Captured output from NET VIEW...\nHere it is:");
Console.WriteLine(s);
 
From the command line, you would write "NET VIEW > MyFile.txt" to save the
output in a file - did you try executing "CMD NET VIEW > MyFile.txt"? I
think this should work (but didn't test it).

Niki
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top