Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Console output to a file

Console output to a file

Scheduled Pinned Locked Moved C#
csharp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sanki779
    wrote on last edited by
    #1

    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

    D L L S 4 Replies Last reply
    0
    • S sanki779

      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

      D Offline
      D Offline
      Diana Fernandez
      wrote on last edited by
      #2

      using System.IO; TextWriter tw = new StreamWriter(@"C:\\Log.txt"); tw.WriteLine("Enter Required Text here"); tw.Flush(); tw.Close(); Hope this helps.

      M 1 Reply Last reply
      0
      • D Diana Fernandez

        using System.IO; TextWriter tw = new StreamWriter(@"C:\\Log.txt"); tw.WriteLine("Enter Required Text here"); tw.Flush(); tw.Close(); Hope this helps.

        M Offline
        M Offline
        Malcolm Smart
        wrote on last edited by
        #3

        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

        S 1 Reply Last reply
        0
        • M Malcolm Smart

          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

          S Offline
          S Offline
          sanki779
          wrote on last edited by
          #4

          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

          M D 2 Replies Last reply
          0
          • S sanki779

            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

            M Offline
            M Offline
            Malcolm Smart
            wrote on last edited by
            #5

            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_

            1 Reply Last reply
            0
            • S sanki779

              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

              D Offline
              D Offline
              Diana Fernandez
              wrote on last edited by
              #6

              Check This link http://www.codeproject.com/dotnet/CSharpPing.asp[^]

              1 Reply Last reply
              0
              • S sanki779

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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] }


                1 Reply Last reply
                0
                • S sanki779

                  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

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  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^

                  **

                  1 Reply Last reply
                  0
                  • S sanki779

                    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

                    S Offline
                    S Offline
                    Sean Michael Murphy
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups