diff options
Diffstat (limited to 'src/WixToolset.BuildTasks/FileSearchHelperMethods.cs')
| -rw-r--r-- | src/WixToolset.BuildTasks/FileSearchHelperMethods.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs b/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs new file mode 100644 index 00000000..6cc804eb --- /dev/null +++ b/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | // 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. | ||
| 2 | |||
| 3 | namespace WixToolset.BuildTasks | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.Collections.Generic; | ||
| 7 | using System.Diagnostics.CodeAnalysis; | ||
| 8 | using System.IO; | ||
| 9 | using System.Text; | ||
| 10 | using Microsoft.Build.Framework; | ||
| 11 | |||
| 12 | /// <summary> | ||
| 13 | /// Contains helper methods on searching for files | ||
| 14 | /// </summary> | ||
| 15 | public static class FileSearchHelperMethods | ||
| 16 | { | ||
| 17 | /// <summary> | ||
| 18 | /// Searches for the existence of a file in multiple directories. | ||
| 19 | /// Search is satisfied if default file path is valid and exists. If not, | ||
| 20 | /// file name is extracted from default path and combined with each of the directories | ||
| 21 | /// looking to see if it exists. If not found, input default path is returned. | ||
| 22 | /// </summary> | ||
| 23 | /// <param name="directories">Array of directories to look in, without filenames in them</param> | ||
| 24 | /// <param name="defaultFullPath">Default path - to use if not found</param> | ||
| 25 | /// <returns>File path if file found. Empty string if not found</returns> | ||
| 26 | public static string SearchFilePaths(string[] directories, string defaultFullPath) | ||
| 27 | { | ||
| 28 | if (String.IsNullOrEmpty(defaultFullPath)) | ||
| 29 | { | ||
| 30 | return String.Empty; | ||
| 31 | } | ||
| 32 | |||
| 33 | if (File.Exists(defaultFullPath)) | ||
| 34 | { | ||
| 35 | return defaultFullPath; | ||
| 36 | } | ||
| 37 | |||
| 38 | if (directories == null) | ||
| 39 | { | ||
| 40 | return string.Empty; | ||
| 41 | } | ||
| 42 | |||
| 43 | string fileName = Path.GetFileName(defaultFullPath); | ||
| 44 | foreach (string currentPath in directories) | ||
| 45 | { | ||
| 46 | if (String.IsNullOrEmpty(currentPath) || String.IsNullOrEmpty(currentPath.Trim())) | ||
| 47 | { | ||
| 48 | continue; | ||
| 49 | } | ||
| 50 | |||
| 51 | if (File.Exists(Path.Combine(currentPath, fileName))) | ||
| 52 | { | ||
| 53 | return Path.Combine(currentPath, fileName); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return String.Empty; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
