简体   繁体   中英

How to avoid automatically console clearing when the printing input stream is really large?

I've written a program printing out all directories on my drive with their full path to the console (Code below). The problem is that the console is getting automatically cleared when the Input stream is getting to large. So I dont get all the paths because after a short time the Console is getting automatically cleared. How can I avoid that?

public static void ListDirectories(DirectoryInfo[] directory)  
    {  
        foreach (DirectoryInfo info in directory)  
        {  
            Console.WriteLine(info.FullName);  
            try  
            {  
                if (info.GetDirectories().Length > 0)  
                    ListDirectories(info.GetDirectories());  
            }  
            catch (UnauthorizedAccessException e)  
            {  
                continue;  
            }  
        }  
    }

This is nothing to do with c#.

Beside that I recommend you to write the output into a txt file or other formats.

Your program is writing information to the Output stream of the process (also known as Standard Output ). For example, Console.WriteLine is the same as Console.Out.WriteLine .

You can redirect that stream of information when you run your program with no changes to the code. When you run the program give it the name of the file you want it to write the stream to, for example, as follows:

YourProgram.exe >YourOutputFile.txt

(You can give a fully-qualified filename if you prefer.) This will overwrite that file each time. If you want it to append to an existing file (if there is one) then use >> rather than > .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM