/// <summary> /// Returns an unresolved qualified path from a component key path. /// </summary> /// <param name="source">A <see cref="PathIntrinsics"/> object.</param> /// <param name="path">The path to convert.</param> /// <returns>An unresolved qualified path.</returns> internal static string GetUnresolvedPSPathFromKeyPath(this PathIntrinsics source, string path) { if (null == source) { throw new ArgumentNullException("source"); } else if (string.IsNullOrEmpty(path)) { // Probably no key path. return null; } // Treat UNC paths as normal FileSystem paths. if (path.StartsWith(DirectorySeparator, StringComparison.Ordinal)) { return source.GetUnresolvedPSPathFromProviderPath(path); } // Get the path prefix to determine the path type. // Windows Installer will sometimes use a ? instead of : so search for both. var pos = path.IndexOfAny(DriveSeparators); // File system key paths. if (1 == pos) { // Translate a ? to a :. if ('?' == path[pos]) { path = path.Substring(0, pos) + DriveSeparator + path.Substring(1 + pos); } return source.GetUnresolvedPSPathFromProviderPath(path); } // Registry key paths. else if (2 == pos) { // Map the key path based on the current process's bitness. var view = RegistryView.GetInstance(); path = view.MapKeyPath(path); if (!string.IsNullOrEmpty(path)) { // Strip the trailing backslash (for registry keys) or full registry value // since the Registry provider cannot represent registry values in a PSPath. pos = path.LastIndexOf(DirectorySeparator, StringComparison.Ordinal); path = path.Substring(0, pos); // Not all the roots have drives, so we have to hard code the provider-qualified root. return RegistryProvider + path; } } // Fallback: not an error, but not valid either. return null; }
/// <summary> /// Returns a <see cref="PSObject"/> wrapper for a <see cref="PatchInstallation"/> object and attaches special properties. /// </summary> /// <param name="source">The <see cref="PatchInstallation"/> object to convert.</param> /// <param name="provider">A <see cref="PathIntrinsics"/> provider used to convert paths.</param> /// <returns>A <see cref="PSObject"/> wrapper with attached special properties.</returns> internal static PSObject ToPSObject(this PatchInstallation source, PathIntrinsics provider = null) { if (null == source) { throw new ArgumentNullException("source"); } var obj = PSObject.AsPSObject(source); // Add path information if possible. if (null != provider) { var path = provider.GetUnresolvedPSPathFromProviderPath(source.LocalPackage); obj.SetPropertyValue<string>("PSPath", path); } return obj; }
public void GetUnresolvedPSPathFromProviderPathThrows() { PathIntrinsics provider = null; provider.GetUnresolvedPSPathFromProviderPath(@"C:\foo"); }