1

I created a string array that utilizes the Directory.GetFiles function which populates the array with .cs file extensions from all sub directories in my project. This is fine, however, I was trying to write these files to a text document while excluding specific files from the array such as "AssemblyInfo.cs" and "TemporaryGeneratedFiles_xxx.cs" using the contains method to filter files with these names. For the most part, the large majority of these files were not written to the text document, however, there are a few cases where these files manage to show up within the text document.

I've tried using a foreach statement instead of a for loop. I've tried playing around with the str.flush(), str.close(), directoryNames.Dispose(), and directoryNames.Close() by putting them in different spots of the for loop. I tried nesting the if statements instead of using the && operator. The || operator doesn't work in this situation. I've tried using the entire file name and only bits and pieces of the file name and none of this seems to work. At one point I did manage to remove all file that had "Temporary" in it but the "AssemblyInfo.cs" files still remain. I even made a method to completely remove anything with the word temporary or assembly in its file name but that also failed.

FileStream directoryNames = new FileStream(dirListPath, FileMode.OpenOrCreate);
StreamWriter str = new StreamWriter(directoryNames);

string[] allFiles = Directory.GetFiles(dirPath, "*.cs", SearchOption.AllDirectories);

for (int i = 0; i < allFiles.Length; i++)
{
    if ((!allFiles[i].Contains("Temporary")) && (!allFiles[i].Contains("Assembly")))
    {
        str.Write(allFiles[i] + Environment.NewLine);
    }
}
str.Flush();
str.Close();
directoryNames.Dispose();
directoryNames.Close();

No error messages occur but as stated above unexpected files pop up where they shouldn't be. Any help would be greatly appreciated.

5
  • Can you show us an example output and desired output of your code? Commented Jul 3, 2019 at 12:32
  • 2
    string.Contains is case sensitive. Here how to have a case insensitive search stackoverflow.com/questions/444798/… or even better using an extension method here stackoverflow.com/questions/17563929/… Commented Jul 3, 2019 at 12:33
  • Please put a breakpoint on str.Write(allFiles[i] + Environment.NewLine);. Run through it until the value of allFiles[i] is a file you don't want added to the file (yes, this may take a while and will be tedious and boring). What is the exact value of allFiles[i] when that occurs? Commented Jul 3, 2019 at 13:38
  • however, there are a few cases where these files manage to show up within the text document. Can you share those few cases with us? Commented Jul 3, 2019 at 13:39
  • Have you considered deleting dirListPath before running your code? I have a sneaking suspicion that using OpenOrCreate means you are writing to the start of the file but that the old contents of the file might be left there, so perhaps you are seeing old text in the file? One way to test my theory would be write THIS IS TO THE END to the stream outside of the loop and then see where that ends up in the file._ Commented Jul 3, 2019 at 13:42

2 Answers 2

0

Thanks to everyone who posted. After some testing mjwillis was correct. Instead of overwriting the contents in the text file it kept writing to the top of the file and adding on to the contents that were written previously. Fixed it by adding

using (FileStream clearTextDocument = File.Create(dirListPath)) { }

Before the FileStream DirectoryNames line. Feel free to post another way to clear a text document if it's more attractive than this.

Sign up to request clarification or add additional context in comments.

Comments

-1

As @steve suggested you code ignores case sensitivity. Try this one.

var allFiles = Directory.EnumerateFiles(dirPath, "*.cs", SearchOption.AllDirectories)
                        .Where(file => file.IndexOf("Temporary", StringComparison.InvariantCultureIgnoreCase) < 0
                                       && file.IndexOf("Assembly", StringComparison.InvariantCultureIgnoreCase) < 0);

File.WriteAllLines(dirListPath, allFiles);

PS: This code makes many changes to your code, but in essence they are same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.