简体   繁体   中英

EnvDTE - How to get the text from a file in a project

I will ask you to please explain your answer, as I'm not acquainted with this Library

So what I'm trying is to is get the text of all project items and count all lines. What is the best way is approaching this problem ?

In order for you to get the count of all lines, you will need to recursively go through each folder and get the length of each files within them.


Process the directories recursively. Get the number of lines for text files and recursively call itself for more directories

    public static int GetTotalLinesInAllFiles(string targetDirectory)
    {
        int totalLines = 0;

        // Process the list of files found in the directory.
        foreach (string fileName in Directory.GetFiles(targetDirectory))
            totalLines += File.ReadAllLines(fileName).Length;

        // Recurse into subdirectories of this directory.
        foreach (string subdirectory in Directory.GetDirectories(targetDirectory))
            totalLines += GetTotalLinesInAllFiles(subdirectory);

        return totalLines;
    }

In your main function, you can start the call with

    ProcessDirectory(Environment.CurrentDirectory);

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