-5

I want to return file names from a specific folder and its subdirectories, but this folder can exist at different paths depending on the drive and Windows version, for example:

  • C:\Program Files\MyApp

  • D:\Program Files\MyApp

  • E:\Program Files\MyApp

  • F:\Program Files\MyApp

  • C:\Program Files (x86)\MyApp

  • D:\Program Files (x86)\MyApp

  • E:\Program Files (x86)\MyApp

  • F:\Program Files (x86)\MyApp

(This folder can exist at only one of those paths.)

This is what I've tried:

Directory.GetFiles("path", "thefiles", SearchOption.AllDirectories)
Sam
  • 7,252
  • 16
  • 46
  • 65
  • so you want to know [where your exe is being run from?](http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application) – Liam Mar 08 '16 at 15:26
  • 1
    It is not us to get used to tag-spammed questions, but you to follow site-rules. For this you should have taken the [tour] already and learned [ask]. If you don't feel like complying or being not rude, you have to live with such comments and possible mod-flags until being banned. – too honest for this site Mar 08 '16 at 15:27
  • @MillieSmith: So do you. At least I don't do it intentionally. – too honest for this site Mar 08 '16 at 15:28

2 Answers2

1

If I understand you correctly, you are looking for the SpecialFolder (https://msdn.microsoft.com/en-us/library/system.environment.specialfolder) enum. Here is an example of how to use it:

Console.WriteLine("ProgramFilesX86: {0}", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86));
Console.WriteLine("ProgramFiles: {0}", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));

Then, just concatenate your project's directory.

You can also get the BaseDirectory of your executable:

Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);

If this is not what you are looking for then please explain your question more as it is not detailed enough.

Tom
  • 2,360
  • 21
  • 15
-1

If you want to use the results of Directory.GetFiles("path", "thefiles", SearchOption.AllDirectories) then I would probably do this (comma separate and add quotes to your "list" of folders):

List<string> foldersToSearch = new List<string>(){C:\Program Files\MyApp

D:\Program Files\MyApp

E:\Pro...

F:\

G:\

H:\

goes on and;

C:\Program Files (x86)\MyApp

D:\Program Files (x86)\MyApp

E:..

F:\

G:\

H:\

I:\};

List<string> filesInSpecificFolder = new List<string>();    

foreach (var folderToSearch in foldersToSearch)
{
    foreach (var file in Directory.GetFiles(folderToSearch, "thefiles", SearchOption.AllDirectories))
    {
        if (Path.GetDirectoryName(file) == "specificFolder")
        { filesInSpecificFolder.Add(file); }
    }
}
Quantic
  • 1,779
  • 19
  • 30
  • @LordPermaximum this is the more general case. The fact that you needed the program files directory was determined by Tom A reading your mind and his code returns the *specific* case of getting that special directory; you didn't mention that anywhere in your post, you just gave a generic list of folders and *some* of them happened to be the program files directory. – Quantic Mar 08 '16 at 16:22
  • I think I couldn't made it clear enough with "..." and "goes on". I was searching for an app directory in the program files but I had to find the correct path for 32-bit Program Files directory depending on different Windows versions and drives first. Still, your solution would work for a list of "specific" folders. That's what I meant. BTW I didn't downvote you. – Lord Permaximum Mar 08 '16 at 16:39