Esempio 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);
        }
        /// <summary>
        /// This method returns the Boolean value for the source string given.
        /// </summary>
        public static bool ParseBoolean(string sourceString, bool defaultValue, bool errorValue)
        {
            // initial value
            bool boolValue = defaultValue;

            try
            {
                // if the string exists
                if (TextHelper.Exists(sourceString))
                {
                    if (TextHelper.IsEqual(sourceString, "1"))
                    {
                        // one is true
                        boolValue = true;
                    }
                    else if (TextHelper.IsEqual(sourceString, "0"))
                    {
                        // zero is false
                        boolValue = false;
                    }
                    else if (TextHelper.IsEqual(sourceString.ToLower(), "true"))
                    {
                        // true
                        boolValue = true;
                    }
                    else if (TextHelper.IsEqual(sourceString.ToLower(), "false"))
                    {
                        // false
                        boolValue = false;
                    }
                    else
                    {
                        // parse as an int
                        int value = NumericHelper.ParseInteger(sourceString, 0, 0);

                        // if value is not equal to zero
                        if (value != 0)
                        {
                            // any non zero is a true
                            boolValue = true;
                        }
                        else
                        {
                            // perform the parse
                            boolValue = Boolean.Parse(sourceString);
                        }
                    }
                }
            }
            catch (Exception error)
            {
                // for debugging only
                string err = error.ToString();

                // set the return value to the errorValue
                boolValue = errorValue;
            }

            // return value
            return(boolValue);
        }