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. Copy text from console window

Copy text from console window

Scheduled Pinned Locked Moved C#
csharpvisual-studiotoolsquestion
16 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.
  • G Offline
    G Offline
    Gene M
    wrote on last edited by
    #1

    Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

    OriginalGriffO L S G 5 Replies Last reply
    0
    • G Gene M

      Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

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

      If this is your own program then write everything to a file as you read it or display on the screen. If it is an app that you do not have the source for then you can redirect all output to a file quite simply with the > operator.

      1 Reply Last reply
      0
      • G Gene M

        Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Provided you can find the process (which isn't complicated even if you didn't launch it) you can use SendKeys to send the CTRL+A, CTRL+C combination to the console app. AFAIK, there isn't a way to "just read the text" from a generic console window after it has been written there. This may help: https://dzone.com/articles/automatically-send-keys-other[^]

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • G Gene M

          Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

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

          Add some smarts to redirect console output as required. [Console.SetOut(TextWriter) Method (System) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.console.setout?view=netframework-4.8) Note that you can then read your own console output if need be (if you redirect to a file).

          The Master said, 'Am I indeed possessed of knowledge? I am not knowing. But if a mean person, who appears quite empty-like, ask anything of me, I set it forth from one end to the other, and exhaust it.' ― Confucian Analects

          1 Reply Last reply
          0
          • G Gene M

            Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

            S Offline
            S Offline
            Sharp Ninja
            wrote on last edited by
            #5

            `CTRL-A` is supposed to select all text in a control or context in Windows. However, it doesn't seem to work in either the PowerShell window or Windows Terminal. I would be very curious about a solution as well.

            L 1 Reply Last reply
            0
            • S Sharp Ninja

              `CTRL-A` is supposed to select all text in a control or context in Windows. However, it doesn't seem to work in either the PowerShell window or Windows Terminal. I would be very curious about a solution as well.

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

              CTRL-A is commonly used to select all text, but it must be implemented by the application itself. It works fine in a cmd window, but powershell ISE/console seems not to work.

              S 1 Reply Last reply
              0
              • G Gene M

                Hi Is there a command to select all text on console window screen? I would like to copy the last screen of text on the console window and save in a text (.scr)file Manually I use Alt + spacebar, E, S to select all and right click the mouse to copy. I run a program in Visual Studio which displays in a console window. Numerous times text is printed to the screen, used and cleared. The last screen of text contains useful information, saved as a script file, and run in a CAD program to draw cabinets and parts. Manual keyboard shortcuts work. A "command" to do the same function would be nice.

                G Offline
                G Offline
                Gene M
                wrote on last edited by
                #7

                Wow you guys are fast. This is my own program. I can look into using SendKey and TextWriter. I might use TextWriter when I start to write the last screen of text or possibly SendKey when the last screen is printed. Sure appreciate it. for console window, to select all, first set up your properties Alt + spacebar (shows the dropdown menu) P (to choose Properties) Options tab- check Quick Edit Mode and Insert Mode Alt + spacebar, E (for Edit), S (for Select All) right click mouse (to copy to clipboard) If you want to paste at command prompt- copy the command- in console window, right click to paste at command prompt. Again, thanks

                L D 2 Replies Last reply
                0
                • G Gene M

                  Wow you guys are fast. This is my own program. I can look into using SendKey and TextWriter. I might use TextWriter when I start to write the last screen of text or possibly SendKey when the last screen is printed. Sure appreciate it. for console window, to select all, first set up your properties Alt + spacebar (shows the dropdown menu) P (to choose Properties) Options tab- check Quick Edit Mode and Insert Mode Alt + spacebar, E (for Edit), S (for Select All) right click mouse (to copy to clipboard) If you want to paste at command prompt- copy the command- in console window, right click to paste at command prompt. Again, thanks

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

                  That description sounds more like a Windows Form application than a Console. Perhaps you should switch to a framework that makes the use of those features easier.

                  1 Reply Last reply
                  0
                  • L Lost User

                    CTRL-A is commonly used to select all text, but it must be implemented by the application itself. It works fine in a cmd window, but powershell ISE/console seems not to work.

                    S Offline
                    S Offline
                    Sharp Ninja
                    wrote on last edited by
                    #9

                    Just did an experiment. I opened CMD.exe and CTRL-A worked. Then I typed powershell.exe and powershell opened in the CMD.exe window. CTRL-A no longer works. Exit powershell to CMD.exe and CTRL-A works again. So PowerShell is eating CTRL-A and not doing anything with it.

                    Richard DeemingR 1 Reply Last reply
                    0
                    • G Gene M

                      Wow you guys are fast. This is my own program. I can look into using SendKey and TextWriter. I might use TextWriter when I start to write the last screen of text or possibly SendKey when the last screen is printed. Sure appreciate it. for console window, to select all, first set up your properties Alt + spacebar (shows the dropdown menu) P (to choose Properties) Options tab- check Quick Edit Mode and Insert Mode Alt + spacebar, E (for Edit), S (for Select All) right click mouse (to copy to clipboard) If you want to paste at command prompt- copy the command- in console window, right click to paste at command prompt. Again, thanks

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Is the console window part of your own app or is it an external to your app?

                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                      Dave Kreskowiak

                      G 1 Reply Last reply
                      0
                      • S Sharp Ninja

                        Just did an experiment. I opened CMD.exe and CTRL-A worked. Then I typed powershell.exe and powershell opened in the CMD.exe window. CTRL-A no longer works. Exit powershell to CMD.exe and CTRL-A works again. So PowerShell is eating CTRL-A and not doing anything with it.

                        Richard DeemingR Offline
                        Richard DeemingR Offline
                        Richard Deeming
                        wrote on last edited by
                        #11

                        The reverse also applies - open Powershell, Ctrl+A doesn't work; run cmd.exe, and Ctrl+A works; exit back to Powershell, and it stops working again. I haven't tried in the new Windows Terminal[^] to see if it's been fixed.


                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                        1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          Is the console window part of your own app or is it an external to your app?

                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                          Dave Kreskowiak

                          G Offline
                          G Offline
                          Gene M
                          wrote on last edited by
                          #12

                          I wrote a program in C# that runs in Visual Studio as a console application. When I run the program, in debug, it runs in a console window.

                          D 1 Reply Last reply
                          0
                          • G Gene M

                            I wrote a program in C# that runs in Visual Studio as a console application. When I run the program, in debug, it runs in a console window.

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #13

                            OK, so then why are you trying to copy'n'paste your own window text? You're going to have to rethink your code and write a module that can write the required text to both the console window and to the file you want to capture the data to.

                            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                            Dave Kreskowiak

                            G 2 Replies Last reply
                            0
                            • D Dave Kreskowiak

                              OK, so then why are you trying to copy'n'paste your own window text? You're going to have to rethink your code and write a module that can write the required text to both the console window and to the file you want to capture the data to.

                              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                              Dave Kreskowiak

                              G Offline
                              G Offline
                              Gene M
                              wrote on last edited by
                              #14

                              I change over to using StreamWriter. I have used it before, I wasn't thinking.

                              using System.IO;
                              using (StreamWriter list = new StreamWriter("C:\\Users\\Gene\\Desktop\\Test.scr", true)) {

                              list.WriteLine("Line " + cabinetx + "," + topy + " @" + cabinetWidth + ",0\\n");
                              

                              }

                              I changed from Console.WriteLine( ) to list.WriteLine( )
                              Works great. Beautiful thing. It doesn't print to screen but it does write to a text file.

                              I use progeCad. (similar to AutoCad)
                              I saw this online. Running script in Cad program
                              It draws a 1" square box. And it did it all in 1 line of code.

                              Line 0,0 @1,0 @0,1 @-1,0 close

                              Starts a line command. Space is like hitting the spacebar or Enter key.
                              Where do you want to start from? x,y coordinate 0,0. Space
                              What's the next point? @1,0. Space. and so forth

                              After that, I drew a cabinet, used it as a model to gather locations and sizes of Left end, right end, top, deck and measured for precise locations and sizes.
                              From those measurements I typed out these commands

                              Rectangle 0,4 @0.75,30.5
                              Rectangle 30,4 @-0.75,30.5
                              Rectangle 0.75,34.5 @28.5,-0.75
                              Rectangle 0.75,4 @28.5,0.75

                              Saved as a script file (.scr) and ran in progeCad as script file

                              It drew out in a split second what I had drawn previously for a model
                              After that, it was like candy. What other parts can I draw?

                              The program is two parts
                              First part gathers information- width, height, depth. Top drawer and 1 door, with 2 adjustable shelves. This is done thru user input, to change location, sizes and choice of parts.
                              Numerous windows display, allowing changes. Window is cleared and the next window is diplayed...

                              Second part is where all the commands code is generated. (similar to lines of code above)
                              I copy the command codes, paste in a text file, save as script file and run in progecad
                              Each line, represents one full command in progeCad. The first line runs and jumps to the next line until the end. It just drew several views of the cabinet.

                              Using StreamWriter, no more copy and pasting.
                              Thanks again.

                              D 1 Reply Last reply
                              0
                              • G Gene M

                                I change over to using StreamWriter. I have used it before, I wasn't thinking.

                                using System.IO;
                                using (StreamWriter list = new StreamWriter("C:\\Users\\Gene\\Desktop\\Test.scr", true)) {

                                list.WriteLine("Line " + cabinetx + "," + topy + " @" + cabinetWidth + ",0\\n");
                                

                                }

                                I changed from Console.WriteLine( ) to list.WriteLine( )
                                Works great. Beautiful thing. It doesn't print to screen but it does write to a text file.

                                I use progeCad. (similar to AutoCad)
                                I saw this online. Running script in Cad program
                                It draws a 1" square box. And it did it all in 1 line of code.

                                Line 0,0 @1,0 @0,1 @-1,0 close

                                Starts a line command. Space is like hitting the spacebar or Enter key.
                                Where do you want to start from? x,y coordinate 0,0. Space
                                What's the next point? @1,0. Space. and so forth

                                After that, I drew a cabinet, used it as a model to gather locations and sizes of Left end, right end, top, deck and measured for precise locations and sizes.
                                From those measurements I typed out these commands

                                Rectangle 0,4 @0.75,30.5
                                Rectangle 30,4 @-0.75,30.5
                                Rectangle 0.75,34.5 @28.5,-0.75
                                Rectangle 0.75,4 @28.5,0.75

                                Saved as a script file (.scr) and ran in progeCad as script file

                                It drew out in a split second what I had drawn previously for a model
                                After that, it was like candy. What other parts can I draw?

                                The program is two parts
                                First part gathers information- width, height, depth. Top drawer and 1 door, with 2 adjustable shelves. This is done thru user input, to change location, sizes and choice of parts.
                                Numerous windows display, allowing changes. Window is cleared and the next window is diplayed...

                                Second part is where all the commands code is generated. (similar to lines of code above)
                                I copy the command codes, paste in a text file, save as script file and run in progecad
                                Each line, represents one full command in progeCad. The first line runs and jumps to the next line until the end. It just drew several views of the cabinet.

                                Using StreamWriter, no more copy and pasting.
                                Thanks again.

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #15

                                OK, so what's wrong with adding a Console.Write immediately after your write to the file?

                                using (StreamWriter list = new StreamWriter("C:\\Users\\Gene\\Desktop\\Test.scr", true)) {
                                string content = "Line " + cabinetx + "," + topy + " @" + cabinetWidth + ",0\n";
                                list.WriteLine(content);
                                Console.Write(content);
                                }

                                Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                Dave Kreskowiak

                                1 Reply Last reply
                                0
                                • D Dave Kreskowiak

                                  OK, so then why are you trying to copy'n'paste your own window text? You're going to have to rethink your code and write a module that can write the required text to both the console window and to the file you want to capture the data to.

                                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                  Dave Kreskowiak

                                  G Offline
                                  G Offline
                                  Gene M
                                  wrote on last edited by
                                  #16

                                  I need to save the text that comes up on the last screen displayed. It's the whole purpose of the program, to generate the text displayed on the last screen. I don't need to see it. I do need to copy and paste it. Before, I have printed the text to the last screen for one reason. To select all- and copy to clipboard then to paste and save in a text document. I needed it displayed so I could copy it. Now, using StreamWriter, that information is written to a text file and saved I no longer have to copy and paste. I don't need to see it. And if I want to see the text from the last page, I can open the text file. It's an exact copy There are several hundred lines I need to change from Console.WriteLine to list.Writeline I have changed several methods that are writing to the text file. It's working as expected. Thanks

                                  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