Exemple #1
0
 /// <summary>
 ///     Checks to see whether the specified path starts with any of the standard paths supported by <see
 ///     cref="ExpandPath"/>, and if so, replaces the prefix with a "$(NAME)" string and returns the resulting value.
 ///     The value passed in should be an absolute path for the substitution to work.</summary>
 public static string UnexpandPath(string path)
 {
     foreach (var folderEnum in EnumStrong.GetValues <Environment.SpecialFolder>())
     {
         var folderPath = Environment.GetFolderPath(folderEnum);
         if (folderPath.Length > 0 && path.StartsWith(folderPath))
         {
             return("$(" + folderEnum + ")" + path.Substring(folderPath.Length));
         }
     }
     if (path.StartsWith(Path.GetTempPath()))
     {
         return("$(Temp)" + path.Substring(Path.GetTempPath().Length));
     }
     return(path);
 }
Exemple #2
0
 /// <summary>
 ///     Expands all occurrences of "$(NAME)" in the specified string with the special folder path for the current
 ///     machine/user. See remarks for details.</summary>
 /// <remarks>
 ///     <para>
 ///         Expands all occurrences of "$(NAME)", where NAME is the name of one of the values of the <see
 ///         cref="Environment.SpecialFolder"/> enum. There is no support for escaping such a replacement, and invalid
 ///         names are ignored.</para>
 ///     <para>
 ///         The following additional names are also recognised:</para>
 ///     <list type="table">
 ///         <item><term>
 ///             $(Temp)</term>
 ///         <description>
 ///             expands to the system's temporary folder path (Path.GetTempPath()).</description></item>
 ///         <item><term>
 ///             $(AppPath)</term>
 ///         <description>
 ///             expands to the directory containing the entry assembly (Assembly.GetEntryAssembly()). Throws an <see
 ///             cref="InvalidOperationException"/> if there is no entry assembly (e.g. in a secondary app domain).</description></item></list></remarks>
 public static string ExpandPath(string path)
 {
     foreach (var folderEnum in EnumStrong.GetValues <Environment.SpecialFolder>())
     {
         path = path.Replace("$(" + folderEnum + ")", Environment.GetFolderPath(folderEnum));
     }
     path = path.Replace("$(Temp)", Path.GetTempPath());
     if (path.Contains("$(AppPath)"))
     {
         if (Assembly.GetEntryAssembly() == null)
         {
             throw new InvalidOperationException("ExpandPath() cannot expand $(AppPath) in an AppDomain where Assembly.GetEntryAssembly() is null.");
         }
         path = path.Replace("$(AppPath)", Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
     }
     return(path);
 }