aboutsummaryrefslogtreecommitdiff
path: root/src/internal/WixToolset.BaseBuildTasks.Sources/FileSearchHelperMethods.cs
blob: 442fedd6be81cbada296f58dfbd56a7809bdca90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.BaseBuildTasks
{
    using System;
    using System.IO;

    /// <summary>
    /// Contains helper methods on searching for files
    /// </summary>
    public static class FileSearchHelperMethods
    {
        /// <summary>
        /// Searches for the existence of a file in multiple directories. 
        /// Search is satisfied if default file path is valid and exists. If not,
        /// file name is extracted from default path and combined with each of the directories
        /// looking to see if it exists. If not found, input default path is returned.
        /// </summary>
        /// <param name="directories">Array of directories to look in, without filenames in them</param>
        /// <param name="defaultFullPath">Default path - to use if not found</param>
        /// <returns>File path if file found. Empty string if not found</returns>
        public static string SearchFilePaths(string[] directories, string defaultFullPath)
        {
            if (String.IsNullOrEmpty(defaultFullPath))
            {
                return String.Empty;
            }

            if (File.Exists(defaultFullPath))
            {
                return defaultFullPath;
            }

            if (directories == null)
            {
                return String.Empty;
            }

            var fileName = Path.GetFileName(defaultFullPath);
            foreach (var currentPath in directories)
            {
                if (String.IsNullOrWhiteSpace(currentPath))
                {
                    continue;
                }

                var path = Path.Combine(currentPath, fileName);
                if (File.Exists(path))
                {
                    return path;
                }
            }

            return String.Empty;
        }
    }
}