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.write line

Console.write line

Scheduled Pinned Locked Moved C#
questioncareer
13 Posts 5 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.
  • N Offline
    N Offline
    nesfrank
    wrote on last edited by
    #1

    Guys, I have a comnosle application that runs as a job in the middle of the night and displays the info via console.write line. I have a question: Is there any way to eather retrive what is written in the screen and write to a file or is there anyway to dump the content of the screen to a file without having to make many modification where ever I have console.write line Please advice guys! Thanks a lot!

    J G L 3 Replies Last reply
    0
    • N nesfrank

      Guys, I have a comnosle application that runs as a job in the middle of the night and displays the info via console.write line. I have a question: Is there any way to eather retrive what is written in the screen and write to a file or is there anyway to dump the content of the screen to a file without having to make many modification where ever I have console.write line Please advice guys! Thanks a lot!

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Console.SetOut[^] You can set the output to any stream (eg a filestream).

      N 1 Reply Last reply
      0
      • J J4amieC

        Console.SetOut[^] You can set the output to any stream (eg a filestream).

        N Offline
        N Offline
        nesfrank
        wrote on last edited by
        #3

        I tried this at the very end of main thread but nothing got written //here call a method that does console.write lines //when done do the code below and file is empty FileStream fs = new FileStream(@"c:\out1.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); Console.SetOut(sw); sw.Close(); fs.Close(); Please advice with some more details

        J 1 Reply Last reply
        0
        • N nesfrank

          Guys, I have a comnosle application that runs as a job in the middle of the night and displays the info via console.write line. I have a question: Is there any way to eather retrive what is written in the screen and write to a file or is there anyway to dump the content of the screen to a file without having to make many modification where ever I have console.write line Please advice guys! Thanks a lot!

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You don't have to change anything at all in the code. You can pipe the output from the program into a file when you run it. To send the output to the file "app.log": TheApplication.exe > app.log To apppend the output (keeping previous results): TheApplication.exe >> app.log If you can't add this from where you run the application, create a batch file (.bat) that contains a line as I showed above, and start the batch file instead of the application.

          Despite everything, the person most likely to be fooling you next is yourself.

          N 1 Reply Last reply
          0
          • G Guffa

            You don't have to change anything at all in the code. You can pipe the output from the program into a file when you run it. To send the output to the file "app.log": TheApplication.exe > app.log To apppend the output (keeping previous results): TheApplication.exe >> app.log If you can't add this from where you run the application, create a batch file (.bat) that contains a line as I showed above, and start the batch file instead of the application.

            Despite everything, the person most likely to be fooling you next is yourself.

            N Offline
            N Offline
            nesfrank
            wrote on last edited by
            #5

            is that supported in c# or is it c++? please send me some more details of code pls.

            V 1 Reply Last reply
            0
            • N nesfrank

              is that supported in c# or is it c++? please send me some more details of code pls.

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #6

              Neither, it's a shell/OS feature. Did you actually try it?

              Cheers, Vıkram.


              "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

              N 1 Reply Last reply
              0
              • V Vikram A Punathambekar

                Neither, it's a shell/OS feature. Did you actually try it?

                Cheers, Vıkram.


                "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

                N Offline
                N Offline
                nesfrank
                wrote on last edited by
                #7

                I am sorry but I don't get what neds to happen. can u send me a bit code like console.write line and out put it? please help me if u can otherwise I would have to collect all console.write line in a string builder.

                G V 2 Replies Last reply
                0
                • N nesfrank

                  I tried this at the very end of main thread but nothing got written //here call a method that does console.write lines //when done do the code below and file is empty FileStream fs = new FileStream(@"c:\out1.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); Console.SetOut(sw); sw.Close(); fs.Close(); Please advice with some more details

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  No the code to set the output stream must go in the console app itself.

                  N 1 Reply Last reply
                  0
                  • J J4amieC

                    No the code to set the output stream must go in the console app itself.

                    N Offline
                    N Offline
                    nesfrank
                    wrote on last edited by
                    #9

                    here is my code class utility { [STAThread] static void Main(string[] args) { ProcessRecords(); //here I placed the code for redirecting to output as above but it did not work. The function ProcessRecords does have some console.writeline statements }

                    V 1 Reply Last reply
                    0
                    • N nesfrank

                      I am sorry but I don't get what neds to happen. can u send me a bit code like console.write line and out put it? please help me if u can otherwise I would have to collect all console.write line in a string builder.

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      Just start your program from a command prompt (cmd.exe), and add " > somfile.txt" after the program name, and the output of the program is sent to the file. This is built into the command shell, and you can for example send a directory listing to a file: dir c:\users\nesfrank\documents > myfiles.txt

                      Despite everything, the person most likely to be fooling you next is yourself.

                      1 Reply Last reply
                      0
                      • N nesfrank

                        I am sorry but I don't get what neds to happen. can u send me a bit code like console.write line and out put it? please help me if u can otherwise I would have to collect all console.write line in a string builder.

                        V Offline
                        V Offline
                        Vikram A Punathambekar
                        wrote on last edited by
                        #11

                        There is NO code to be written. Your app (let's call it Foo.exe) has several Console.WriteLine() statements which write to the console, and you want them to be written to a file (let's call it AppLog.txt). Right now, this is how you would invoke your app:

                        C:\>Foo.exe

                        Simply change that to

                        C:\>Foo.exe > AppLog.txt

                        If you want the app to append to the file rather than overwrite, use

                        C:\>Foo.exe >> AppLog.txt

                        Obviously, you will have to use the correct path, etc.

                        Cheers, Vıkram.


                        "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

                        1 Reply Last reply
                        0
                        • N nesfrank

                          here is my code class utility { [STAThread] static void Main(string[] args) { ProcessRecords(); //here I placed the code for redirecting to output as above but it did not work. The function ProcessRecords does have some console.writeline statements }

                          V Offline
                          V Offline
                          Vikram A Punathambekar
                          wrote on last edited by
                          #12

                          nesfrank wrote:

                          here I placed the code for redirecting to output as above but it did not work. The function ProcessRecords does have some console.writeline statements

                          Except the Console.WriteLine() statements have already been written to the console :doh: You must do it before you actually do anything else. I suggest you move it to the beginning of Main().

                          Cheers, Vıkram.


                          "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

                          1 Reply Last reply
                          0
                          • N nesfrank

                            Guys, I have a comnosle application that runs as a job in the middle of the night and displays the info via console.write line. I have a question: Is there any way to eather retrive what is written in the screen and write to a file or is there anyway to dump the content of the screen to a file without having to make many modification where ever I have console.write line Please advice guys! Thanks a lot!

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #13

                            Additionally to what the others already said, there's also a slightly more advanced, yet more flexible solution: log4net[^] It has some nice features: - you can specify where the log messages should go (like to the console, to a file, via mail and much more) - you can add a custom format to your log messages (like a timestamp) - you can specify the type of the log message, like Info, Warning, Error, Debug and much more regards

                            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