File size: 4,842 Bytes
8c763fb | 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace System.Management.Automation
{
/// <summary>
/// An object that represents a path.
/// </summary>
public sealed class PathInfo
{
/// <summary>
/// Gets the drive that contains the path.
/// </summary>
public PSDriveInfo Drive
{
get
{
PSDriveInfo result = null;
if (_drive != null &&
!_drive.Hidden)
{
result = _drive;
}
return result;
}
}
/// <summary>
/// Gets the provider that contains the path.
/// </summary>
public ProviderInfo Provider
{
get
{
return _provider;
}
}
/// <summary>
/// This is the internal mechanism to get the hidden drive.
/// </summary>
/// <returns>
/// The drive associated with this PathInfo.
/// </returns>
internal PSDriveInfo GetDrive()
{
return _drive;
}
/// <summary>
/// Gets the provider internal path for the PSPath that this PathInfo represents.
/// </summary>
/// <exception cref="ProviderInvocationException">
/// The provider encountered an error when resolving the path.
/// </exception>
/// <exception cref="InvalidOperationException">
/// The path was a home relative path but the home path was not
/// set for the provider.
/// </exception>
public string ProviderPath
{
get
{
if (_providerPath == null)
{
// Construct the providerPath
LocationGlobber pathGlobber = _sessionState.Internal.ExecutionContext.LocationGlobber;
_providerPath = pathGlobber.GetProviderPath(Path);
}
return _providerPath;
}
}
private string _providerPath;
private readonly SessionState _sessionState;
/// <summary>
/// Gets the PowerShell path that this object represents.
/// </summary>
public string Path
{
get
{
return this.ToString();
}
}
private readonly PSDriveInfo _drive;
private readonly ProviderInfo _provider;
private readonly string _path = string.Empty;
/// <summary>
/// Gets a string representing the PowerShell path.
/// </summary>
/// <returns>
/// A string representing the PowerShell path.
/// </returns>
public override string ToString()
{
string result = _path;
if (_drive == null ||
_drive.Hidden)
{
// For hidden drives just return the current location
result =
LocationGlobber.GetProviderQualifiedPath(
_path,
_provider);
}
else
{
result = LocationGlobber.GetDriveQualifiedPath(_path, _drive);
}
return result;
}
/// <summary>
/// The constructor of the PathInfo object.
/// </summary>
/// <param name="drive">
/// The drive that contains the path
/// </param>
/// <param name="provider">
/// The provider that contains the path.
/// </param>
/// <param name="path">
/// The path this object represents.
/// </param>
/// <param name="sessionState">
/// The session state associated with the drive, provider, and path information.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="drive"/>, <paramref name="provider"/>,
/// <paramref name="path"/>, or <paramref name="sessionState"/> is null.
/// </exception>
internal PathInfo(PSDriveInfo drive, ProviderInfo provider, string path, SessionState sessionState)
{
if (provider == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(provider));
}
if (path == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(path));
}
if (sessionState == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(sessionState));
}
_drive = drive;
_provider = provider;
_path = path;
_sessionState = sessionState;
}
}
}
|