Esempio n. 1
0
        /// <summary>
        /// Locate file with one of possible extensions in search directories.
        /// <br/><br/>
        /// If file can't be found, empty string will be returned.
        /// </summary>
        /// <param name="file">File name with or without extension and leading path</param>
        /// <param name="search">Directory search list</param>
        /// <param name="extension">List of filename extensions to check (i.e. ".txt", "ini", ".")</param>
        /// <param name="behaviour">Lookup behaviour (iterate over directories or extensions)</param>
        /// <returns>Empty string if file not found or full path to found one</returns>
        public static string Locate(string file, string[] search, string[] extension, Energy.Enumeration.LocateBehaviour behaviour)
        {
            if (string.IsNullOrEmpty(file))
            {
                return("");
            }

            file = Energy.Base.Path.ChangeSeparator(file);

            if (search == null || search.Length == 0)
            {
                search = new string[] { "" };
            }

            if (search.Length > 0)
            {
                for (int i = 0; i < search.Length; i++)
                {
                    search[i] = Energy.Base.Path.ChangeSeparator(search[i]);
                }
            }

            if (extension == null || extension.Length == 0)
            {
                extension = new string[] { "" };
            }

            string fileExtension = System.IO.Path.GetExtension(file);

            if (fileExtension.Length > 0)
            {
                string[] array = new string[extension.Length + 1];
                array[0] = fileExtension;
                Array.Copy(extension, 0, array, 1, extension.Length);
                extension = array;
            }

            if (file.EndsWith("."))
            {
                file = file.Substring(0, file.Length - 1);
                if (file.Length == 0)
                {
                    return("");
                }
            }

            switch (behaviour)
            {
            case Energy.Enumeration.LocateBehaviour.Default:
            case Energy.Enumeration.LocateBehaviour.Directories:

                for (int i = 0; i < search.Length; i++)
                {
                    string directory = search[i];

                    if (string.IsNullOrEmpty(directory))
                    {
                        directory = System.IO.Directory.GetCurrentDirectory();
                    }

                    try
                    {
                        foreach (string ext in extension)
                        {
                            string candidate = System.IO.Path.Combine(directory, file);

                            if (!string.IsNullOrEmpty(ext) && 0 != string.Compare(".", ext, false))
                            {
                                candidate = System.IO.Path.ChangeExtension(candidate, ext);
                            }

                            if (System.IO.File.Exists(candidate))
                            {
                                return(candidate);
                            }
                        }
                    }
                    catch (Exception x)
                    {
                        Energy.Core.Bug.Catch(x);
                    }
                }

                break;

            case Energy.Enumeration.LocateBehaviour.Extensions:

                foreach (string ext in extension)
                {
                    foreach (string directory in search)
                    {
                        try
                        {
                            string candidate = System.IO.Path.Combine(directory, file);

                            if (!string.IsNullOrEmpty(ext) && 0 == string.Compare(".", ext, false))
                            {
                                candidate = System.IO.Path.ChangeExtension(candidate, ext);
                            }

                            if (System.IO.File.Exists(candidate))
                            {
                                return(candidate);
                            }
                        }
                        catch (Exception x)
                        {
                            Energy.Core.Bug.Catch(x);
                        }
                    }
                }

                break;
            }

            return("");
        }
Esempio n. 2
0
        /// <summary>
        /// Locate file with one of possible extensions in search directory.
        /// <br/><br/>
        /// If file can't be found, empty string will be returned.
        /// </summary>
        /// <param name="list">Array of file names with or without extension and leading path</param>
        /// <param name="search">Directory search list</param>
        /// <param name="extension">List of filename extensions to check (i.e. ".txt", "ini", ".")</param>
        /// <param name="behaviour">Lookup behaviour (iterate over directories or extensions)</param>
        /// <returns>Empty string if file not found or full path to found one</returns>
        public static string Locate(string[] list, string[] search, string[] extension, Energy.Enumeration.LocateBehaviour behaviour)
        {
            if (null == list)
            {
                return("");
            }
            string result = "";

            foreach (string file in list)
            {
                result = Locate(file, search, extension, behaviour);
                if (string.IsNullOrEmpty(result))
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
            return(result);
        }