Ejemplo n.º 1
0
        /// ------------------------------------------------------------------------------------
        private static string LocateInProgramFilesFolder(string exeName, SearchOption srcOption,
                                                         params string[] subFoldersToSearch)
        {
            if (subFoldersToSearch.Length == 0)
            {
                subFoldersToSearch = new[] { string.Empty }
            }
            ;

            foreach (var progFolder in Enumerable.Where <string>(GetPossibleProgramFilesFolders(), Directory.Exists))
            {
                // calling Directory.GetFiles("C:\Program Files", exeName, SearchOption.AllDirectories) will fail
                // if even one of the children of the Program Files doesn't allow you to search it.
                // So instead we first gather up all the children directories, and then search those.
                // Some will give us access denied, and that's fine, we skip them.
                // But we don't want to look in child directories on Linux because GetPossibleProgramFilesFolders()
                // gives us the individual elements of the PATH environment variable, and these specify exactly
                // those directories that should be searched for program files.
                foreach (var path in subFoldersToSearch.Select(sf => Path.Combine(progFolder, sf)).Where(Directory.Exists))
                {
                    if (Platform.IsWindows)
                    {
                        foreach (var subDir in DirectoryHelper.GetSafeDirectories(path))
                        {
                            var tgtPath = GetFiles(exeName, srcOption, subDir);
                            if (!string.IsNullOrEmpty(tgtPath))
                            {
                                return(tgtPath);
                            }
                        }
                    }
                    else
                    {
                        var tgtPath = GetFiles(exeName, srcOption, path);
                        if (!string.IsNullOrEmpty(tgtPath))
                        {
                            return(tgtPath);
                        }
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
 public static string[] GetSafeDirectories(string path)
 {
     return(DirectoryHelper.GetSafeDirectories(path));
 }