Exemplo n.º 1
0
        /// <summary>
        /// This method returns a list of file names.
        /// </summary>
        /// <param name="directoryPath">The path to the Directory containing the files</param>
        /// <param name="filterExtensions">This override accepts a list of filters instead of just one so that mutliple types can be found</param>
        /// will be returned.</param>
        /// <param name="removeExtension">If true, the extension is removed</param>
        /// <returns></returns>
        public static List <string> GetFiles(string directoryPath, List <string> filterExtensions = null, bool removeExtension = false)
        {
            // initial value
            List <string> files = new List <string>();

            // local
            string extension = "";

            // If the filePath string exists
            if ((TextHelper.Exists(directoryPath)) && (Directory.Exists(directoryPath)))
            {
                // get the fileNames
                string[] fileNames = Directory.GetFiles(directoryPath);

                // if the fileNames exist
                if ((fileNames != null) && (fileNames.Length > 0))
                {
                    // if the value for removeExtension is true
                    if (removeExtension)
                    {
                        // get a tempList
                        List <string> tempNames = fileNames.ToList();

                        // Iterate the collection of string objects
                        foreach (string tempName in fileNames)
                        {
                            // get the name without the extension
                            string name = GetFileNameWithoutExtensionEx(tempName, ref extension);

                            // If the filterExtension string exists
                            if (ListHelper.HasOneOrMoreItems(filterExtensions))
                            {
                                // Iterate the collection of string objects
                                foreach (string filterExtension in filterExtensions)
                                {
                                    // if the extension exists
                                    if (TextHelper.IsEqual(filterExtension, extension))
                                    {
                                        // Add this file
                                        files.Add(name);
                                    }
                                }
                            }
                            else
                            {
                                // Add this file
                                files.Add(name);
                            }
                        }
                    }
                    // if the filterExtension exists
                    else if (ListHelper.HasOneOrMoreItems(filterExtensions))
                    {
                        // get a tempList
                        List <string> tempNames = fileNames.ToList();

                        // Iterate the collection of string objects
                        foreach (string tempName in fileNames)
                        {
                            // The name is not needed here, just getting the extension
                            GetFileNameWithoutExtensionEx(tempName, ref extension);

                            // Iterate the collection of string objects
                            foreach (string filterExtension in filterExtensions)
                            {
                                // if the extension exists
                                if (TextHelper.IsEqual(filterExtension, extension))
                                {
                                    // Add this file
                                    files.Add(tempName);
                                }
                            }
                        }
                    }
                    else
                    {
                        // get a tempList
                        List <string> tempNames = fileNames.ToList();

                        // Iterate the collection of string objects
                        foreach (string tempName in fileNames)
                        {
                            // Add this file
                            files.Add(tempName);
                        }
                    }
                }
            }

            // return value
            return(files);
        }