private string GetProviderRootFromSpecifiedRoot(string root, ProviderInfo provider) { string str = root; SessionState state = new SessionState(this._context.TopLevelSessionState); Collection<string> resolvedProviderPathFromPSPath = null; ProviderInfo info = null; try { resolvedProviderPathFromPSPath = state.Path.GetResolvedProviderPathFromPSPath(root, out info); if (((resolvedProviderPathFromPSPath != null) && (resolvedProviderPathFromPSPath.Count == 1)) && provider.NameEquals(info.FullName)) { ProviderIntrinsics intrinsics = new ProviderIntrinsics(this); if (intrinsics.Item.Exists(root)) { str = resolvedProviderPathFromPSPath[0]; } } } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (System.Management.Automation.DriveNotFoundException) { } catch (ProviderNotFoundException) { } catch (ItemNotFoundException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } catch (ProviderInvocationException) { } catch (ArgumentException) { } return str; }
/// <summary> /// Tries to resolve the drive root as an MSH path. If it successfully resolves /// to a single path then the resolved provider internal path is returned. If it /// does not resolve to a single MSH path the root is returned as it was passed. /// </summary> /// /// <param name="root"> /// The root path of the drive to be resolved. /// </param> /// /// <param name="provider"> /// The provider that should be used when resolving the path. /// </param> /// /// <returns> /// The new root path of the drive. /// </returns> /// private string GetProviderRootFromSpecifiedRoot(string root, ProviderInfo provider) { Dbg.Diagnostics.Assert( root != null, "Caller should have verified the root"); Dbg.Diagnostics.Assert( provider != null, "Caller should have verified the provider"); string result = root; SessionState sessionState = new SessionState(ExecutionContext.TopLevelSessionState); Collection<string> resolvedPaths = null; ProviderInfo resolvedProvider = null; try { // First try to resolve the root as an MSH path resolvedPaths = sessionState.Path.GetResolvedProviderPathFromPSPath(root, out resolvedProvider); // If a single path was resolved... if (resolvedPaths != null && resolvedPaths.Count == 1) { // and the provider used to resolve the path, // matches the one specified by the drive... if (provider.NameEquals(resolvedProvider.FullName)) { // and the item exists ProviderIntrinsics providerIntrinsics = new ProviderIntrinsics(this); if (providerIntrinsics.Item.Exists(root)) { // then use the resolved path as the root of the drive result = resolvedPaths[0]; } } } } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } // If any of the following exceptions are thrown we assume that // the path is a file system path not an MSH path and try // to create the drive with that root. catch (DriveNotFoundException) { } catch (ProviderNotFoundException) { } catch (ItemNotFoundException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } catch (ProviderInvocationException) { } catch (ArgumentException) { } return result; } // GetProviderRootFromSpecifiedRoot