0

Could anyone please clarify my doubts?

Scenario: i want to check more than one extension files exists in a given path in a single condition.

DirectoryInfo projectInfo = new DirectoryInfo(projectPath);
string projectFileType = "*.vbproj,*.csproj,*.master,*.aspx";
if (projectFileType != string.Empty)
 {
  FileInfo[] projFiles = projectInfo.GetFiles(projectFileType, SearchOption.AllDirectories);
 } 

Thanks in Advance!!

Karthi
  • 575
  • 2
  • 10
  • 29
  • 1
    possible duplicate of [DirectoryInfo.GetFiles, How to get different types of files in C#](http://stackoverflow.com/questions/14961437/directoryinfo-getfiles-how-to-get-different-types-of-files-in-c-sharp) – Panagiotis Kanavos Mar 03 '15 at 07:50
  • You'll find that this question has already been asked many times. The duplicate question contains both answers and more pointers to previous similar questions. In short, either concatenate the results of each search, or enumerate all files checking their extension against a list of extensions – Panagiotis Kanavos Mar 03 '15 at 07:52

3 Answers3

1

Your question is not clear. I assume you are checking for the existance of the files of type ".vbproj,.csproj,.master,.aspx" in a specific folder. For this you can use the below code.

        bool IsAllFilesAvailable(string directoryPath)
        {
            var filteredFiles = Directory
                   .EnumerateFiles(directoryPath)
                   .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("vbproj") || file.ToLower().EndsWith("csproj") || file.ToLower().EndsWith("master"))
                   .ToList();            

            if (filteredFiles.Count == 4)
                return true;
            else
                return false;
        }
IMK
  • 654
  • 9
  • 15
  • This is a very bad idea - enumerating all files instead of those with a specific extension. This will waste a lot of time *and* can return wrong results - the code doesn't check for the actual extension, only if the file ends with some string – Panagiotis Kanavos Mar 03 '15 at 07:48
0

Your code will not work. GetFiles takes only one file extension. That's a limitation of the GetFiles method. The solution is to write your own code.

You can use this code:

var filteredFiles = Directory
                   .EnumerateFiles(path)
                   .Where(file => file.ToLower().EndsWith("aspx") || file.ToLower().EndsWith("vbproj") || file.ToLower().EndsWith("csproj") || file.ToLower().EndsWith("master"))
                   .ToList();
FileInfo[] projFiles = filteredFiles.ToArray();

EDIT: A more efficient way to do this, as suggested by Panagiotis Kanavos in the comments, is show below:

string supportedExtensions = "*.aspx,*.vbproj,*.csproj,*.master";
FileInfo[] projFiles = Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)
.Where(s => supportedExtensions.Contains(s.Extension.ToLower())).ToArray();
Community
  • 1
  • 1
Utsav Dawn
  • 7,896
  • 2
  • 29
  • 44
  • This is a very bad idea - enumerating all files instead of those with a specific extension. This will waste a lot of time *and* can return wrong results - the code doesn't check for the actual extension, only if the file ends with some string – Panagiotis Kanavos Mar 03 '15 at 07:48
  • Can you give a better solution? – Utsav Dawn Mar 03 '15 at 08:00
  • Use the Extension property of FileInfo. Concatenate the search results. Use a list of extensions with case-invariant comparison. This question has already been answered multiple times. The duplicates contain all these techniques. – Panagiotis Kanavos Mar 03 '15 at 08:06
  • What is the reference dll i need to add for "Extension"? – Karthi Mar 03 '15 at 09:24
  • No need to add any reference dll. It is found in System.IO.FileInfo namespace. – Utsav Dawn Mar 03 '15 at 09:31
0

We can use the below code as solution.

  FileInfo[] fi = new string[]{"*.csproj", "*.vbproj", "*.master", "*.aspx", "*.config", "*.ascx", "*.resx"}
        .SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories))
        .Distinct().ToArray();
Karthi
  • 575
  • 2
  • 10
  • 29