Console output to a file
-
Hi , I have created an application in C# which runs varios windows commands. For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file. Thanks in Advance
Sankalp Verma
-
Hi , I have created an application in C# which runs varios windows commands. For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file. Thanks in Advance
Sankalp Verma
using System.IO; TextWriter tw = new StreamWriter(@"C:\\Log.txt"); tw.WriteLine("Enter Required Text here"); tw.Flush(); tw.Close();
Hope this helps. -
using System.IO; TextWriter tw = new StreamWriter(@"C:\\Log.txt"); tw.WriteLine("Enter Required Text here"); tw.Flush(); tw.Close();
Hope this helps.Look at the TRACE namespace. You can set tracelisteners up to echo stuff to the console, to DbgVwr and a text file.
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
-
Look at the TRACE namespace. You can set tracelisteners up to echo stuff to the console, to DbgVwr and a text file.
"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF
Thanks all for your valuable input but I think that i was not able to explain the problem properly. Suppose I am using C# to run the ping command. Now once this command runs all output I get goes to the console window. Now what I want is that In place of going to the console window, can it go into a log file. Thanks and Regards,
Sankalp Verma
-
Thanks all for your valuable input but I think that i was not able to explain the problem properly. Suppose I am using C# to run the ping command. Now once this command runs all output I get goes to the console window. Now what I want is that In place of going to the console window, can it go into a log file. Thanks and Regards,
Sankalp Verma
OK - specifically for PING you can use the
System.Net.NetworkInformation.Ping
. You can query this for the results of the ping. For capturing any console output from a 'shelled' program, do something like this
System.Diagnostics.Process myProc = new System.Diagnostics.Process(); myProc.StartInfo.FileName = String.Format("myprogram.exe"); myProc.StartInfo.Arguments = "-args myargs"; myProc.StartInfo.UseShellExecute = false; myProc.StartInfo.RedirectStandardOutput = true; myProc.Start(); myProc.WaitForExit(1000 * 20); string output = myProc.StandardOutput.ReadToEnd(); //output now contains everything output by myprogram.exe _"More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF_
-
Thanks all for your valuable input but I think that i was not able to explain the problem properly. Suppose I am using C# to run the ping command. Now once this command runs all output I get goes to the console window. Now what I want is that In place of going to the console window, can it go into a log file. Thanks and Regards,
Sankalp Verma
Check This link http://www.codeproject.com/dotnet/CSharpPing.asp[^]
-
Hi , I have created an application in C# which runs varios windows commands. For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file. Thanks in Advance
Sankalp Verma
Hi, read up on the Process class, especially Process.StandardOutput and StandardError. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi , I have created an application in C# which runs varios windows commands. For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file. Thanks in Advance
Sankalp Verma
Console.SetOut(File.CreateText("C:/log.txt"));
**
xacc.ide-0.2.0.77 - now with C# 3.5 support and Navigation Bar!^
New xacc.ide release RSS feed^**
-
Hi , I have created an application in C# which runs varios windows commands. For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file. Thanks in Advance
Sankalp Verma
sanki779 wrote:
For the Debugging purpose I need that whatever output I am getting to be pasted to a log file also say to C:\Log.txt file. How is it possible to redirect all the console output to a file.
If your app doesn't accept any input, don't modify it at all. Just call it from the command line and redirect the output using DOS to wherever you want.
c:\>myapp.exe > c:\log.txt
If your app needs input, naturally this won't work. Share and enjoy. Sean