Beispiel #1
0
        private static ApplicationExecutableInfo GetApplicationExecutable(DirectoryInfo directory)
        {
            DirectoryInfo currentlyTargetedFolder = directory;

            // Keyword formatting and splitting
            List <string> keywords = StringMatcher.FormatKeyword(directory.Name);

            List <string> executableKeywords = new List <string>(keywords);

            executableKeywords.AddRange(priorityExecutableNames);

            List <string> folderKeywords = new List <string>(keywords);

            folderKeywords.AddRange(priorityFolderNames);

            // The return var
            ApplicationExecutableInfo temp = new ApplicationExecutableInfo
            {
                debug = new ApplicationExecutableInfoDebug
                {
                    keywords = keywords
                }
            };

            // Get executables in root
            FileInfo[] subExecutables = directory.GetFiles("*.exe");

            FileInfo tempFileInfo = SearchFolderForExecutable(directory, executableKeywords);

            if (tempFileInfo != null)
            {
                temp.directoryType         = ApplicationDirectoryType.Application;
                temp.applicationExecutable = tempFileInfo;
                return(temp);
            }

            // Get sub directories in root
            DirectoryInfo[] subDirectories = directory.GetDirectories();

            // If there's no files or folders return empty folder
            if (subDirectories.Length == 0 && subExecutables.Length == 0)
            {
                temp.directoryType = ApplicationDirectoryType.Empty;
                return(temp);
            }

            // Check if the intallation is in a single subfolder
            if (subDirectories.Length == 1)
            {
                // Check if the intallation is in a single subfolder by matching keywords or checking if has a version number
                if (StringMatcher.IsMatch(subDirectories[0].Name, folderKeywords) || Regex.IsMatch(subDirectories[0].Name, @"\d"))
                {
                    currentlyTargetedFolder = temp.subdirectory = subDirectories[0];

                    FileInfo tempFileInfo2 = SearchFolderForExecutable(currentlyTargetedFolder, executableKeywords);
                    if (tempFileInfo2 != null)
                    {
                        temp.directoryType         = ApplicationDirectoryType.Application;
                        temp.applicationExecutable = tempFileInfo2;
                        return(temp);
                    }

                    subDirectories = currentlyTargetedFolder.GetDirectories();
                }
                if (subExecutables.Length == 0)
                {
                    temp.directoryType = ApplicationDirectoryType.Company;
                    return(temp);
                }
            }

            // Check if multiple subdirectories match with directory name then it's probably a company folder
            if (subExecutables.Length == 0)
            {
                int numberOfMatchingSubdirectories = 0;
                for (int i = 0; i < subDirectories.Length; i++)
                {
                    if (subDirectories[i].Name.ToLower().Contains(directory.Name.ToLower()))
                    {
                        numberOfMatchingSubdirectories++;
                    }
                }
                Console.WriteLine(directory.Name + " empty as shit but it has " + subDirectories.Length + " subfolders and " + numberOfMatchingSubdirectories + " subdirs match parent");
                if ((numberOfMatchingSubdirectories / subDirectories.Length) > 0.4f)
                {
                    Console.WriteLine(directory.Name + "is a company");
                    temp.directoryType = ApplicationDirectoryType.Company;
                    return(temp);
                }
            }

            // Check sub directories with prioritized names
            for (int x = 0; x < subDirectories.Length; x++)
            {
                for (int y = 0; y < folderKeywords.Count; y++)
                {
                    if (subDirectories[x].Name.ToLower().Contains(folderKeywords[y].ToLower()))
                    {
                        FileInfo tempfileinto = SearchFolderForExecutable(subDirectories[x], executableKeywords);
                        if (tempfileinto != null)
                        {
                            temp.directoryType         = ApplicationDirectoryType.Application;
                            temp.applicationExecutable = tempfileinto;
                            return(temp);
                        }
                    }
                }
            }

            // Do full check
            if (Settings.searchMethod == SearchMethod.Full)
            {
                // Search all directories for executable
                for (int i = 0; i < subDirectories.Length; i++)
                {
                    FileInfo tempfileinto = SearchFolderForExecutable(subDirectories[i], executableKeywords);
                    if (tempfileinto != null)
                    {
                        temp.directoryType         = ApplicationDirectoryType.Application;
                        temp.applicationExecutable = tempfileinto;
                        return(temp);
                    }
                }
                //Get the executable with shortest name in root directory
                if (subExecutables.Length > 0)
                {
                    if (subExecutables.Length == 1)
                    {
                        temp.directoryType         = ApplicationDirectoryType.Application;
                        temp.applicationExecutable = subExecutables[0];
                        return(temp);
                    }
                    else
                    {
                        FileInfo executableWithShortestName = null;

                        for (int i = 0; i < subExecutables.Length; i++)
                        {
                            if (!StringMatcher.IsMatch(subExecutables[i].Name, excludedExecutableNames.ToList()))
                            {
                                if (executableWithShortestName == null)
                                {
                                    executableWithShortestName = subExecutables[i];
                                }
                                else if (executableWithShortestName.Name.Length > subExecutables[i].Name.Length)
                                {
                                    executableWithShortestName = subExecutables[i];
                                }
                            }
                        }
                        if (executableWithShortestName != null)
                        {
                            temp.directoryType         = ApplicationDirectoryType.Application;
                            temp.applicationExecutable = executableWithShortestName;
                            return(temp);
                        }
                    }
                }
            }


            // Company folder
            temp.directoryType = ApplicationDirectoryType.Company;
            return(temp);
        }
Beispiel #2
0
            public ApplicationInfoContainer(DirectoryInfo directory)
            {
                ApplicationDirectory = directory;
                ApplicationName      = GetApplicationName(directory.Name);

                ApplicationExecutableInfo tempExeInfo = GetApplicationExecutable(directory);

                debug = tempExeInfo.debug;

                switch (tempExeInfo.directoryType)
                {
                case ApplicationDirectoryType.Empty:

                    break;

                case ApplicationDirectoryType.Application:
                    ApplicationExecutable = tempExeInfo.applicationExecutable;

                    break;

                case ApplicationDirectoryType.Company:
                    IsCompany = true;
                    children  = new List <ApplicationInfoContainer>();
                    DirectoryInfo[] subdirectories;

                    if (tempExeInfo.subdirectory != null)
                    {
                        subdirectories = tempExeInfo.subdirectory.GetDirectories();
                    }
                    else
                    {
                        subdirectories = directory.GetDirectories();
                    }

                    for (int i = 0; i < subdirectories.Length; i++)
                    {
                        children.Add(new ApplicationInfoContainer(subdirectories[i]));
                        children[children.Count - 1].ParentName = ApplicationName;
                    }
                    break;

                default:
                    break;
                }


                if (ApplicationExecutable != null)
                {
                    ApplicationPath        = ApplicationExecutable.FullName;
                    ApplicationDescription = FileVersionInfo.GetVersionInfo(ApplicationExecutable.FullName).FileDescription;
                    ApplicationIcon        = GetApplicationIcon(ApplicationExecutable.FullName);
                }
                else
                {
                    ApplicationIcon = GetApplicationIcon("");
                }


                if (ApplicationIcon.CanFreeze)
                {
                    ApplicationIcon.Freeze();
                }
            }