本文整理汇总了C#中System.Management.Path.TryGetDriveName方法的典型用法代码示例。如果您正苦于以下问题:C# Path.TryGetDriveName方法的具体用法?C# Path.TryGetDriveName怎么用?C# Path.TryGetDriveName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Path
的用法示例。
在下文中一共展示了Path.TryGetDriveName方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PathIsAbsolute
private bool PathIsAbsolute(Path path)
{
string drive = null;
if (path.TryGetDriveName(out drive))
{
// TODO: workaround to be removed when we completeley remove the Path class and
// use PathIntrinsics instead
if (System.IO.Path.DirectorySeparatorChar == '\\' && path.StartsWithSlash())
{
return false;
}
return true;
}
return false;
}
示例2: SetLocation
internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime)
{
// TODO: deal with paths starting with ".\"
if (path == null)
{
throw new NullReferenceException("Path can't be null");
}
if (path == "~")
{
// Older Mono versions (sadly the one that's currently still
// available) have a bug where GetFolderPath returns an empty
// string for most SpecialFolder values, but only on
// non-Windows.
// See: https://bugzilla.xamarin.com/show_bug.cgi?id=2873
path = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
// HACK: Use $Env:HOME until Mono 2.10 dies out.
if (path == "")
path = Environment.GetEnvironmentVariable("HOME");
}
PSDriveInfo nextDrive = CurrentDrive;
path = path.NormalizeSlashes();
string driveName = null;
if (path.TryGetDriveName(out driveName))
{
try
{
nextDrive = _executionContext.SessionState.Drive.Get(driveName);
}
catch (MethodInvocationException) //didn't find a drive (maybe it's "\" on Windows)
{
nextDrive = CurrentDrive;
}
}
Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path);
// I'm not a fan of this block of code.
// The goal here is to throw an exception if trying to "CD" into an invalid location
//
// Not sure why the providerInstances are returned as a collection. Feels like given a
// path we should have one provider we're talking to.
if (_providerInstances.ContainsKey(nextDrive.Provider.Name))
{
bool pathExists = false;
IEnumerable<ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast<ItemCmdletProvider>();
ItemCmdletProvider currentProvider = null;
foreach (var provider in cmdletProviders)
{
if (provider.ItemExists(newLocation, providerRuntime))
{
pathExists = true;
currentProvider = provider;
break;
}
}
if (!pathExists)
{
throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation));
}
else
{
if (currentProvider is FileSystemProvider)
{
System.Environment.CurrentDirectory = newLocation;
}
}
}
else
{
throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name);
}
nextDrive.CurrentLocation = newLocation;
CurrentDrive = nextDrive;
_providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive;
return CurrentLocation;
}
示例3: SetLocation
internal PathInfo SetLocation(Path path, ProviderRuntime providerRuntime)
{
// TODO: deal with paths starting with ".\"
if (path == null)
{
throw new NullReferenceException("Path can't be null");
}
PSDriveInfo nextDrive = CurrentDrive;
path = path.NormalizeSlashes();
string driveName = null;
if (path.TryGetDriveName(out driveName))
{
nextDrive = GetDrive(driveName);
}
if (nextDrive == null)
{
nextDrive = CurrentDrive;
}
Path newLocation = PathNavigation.CalculateFullPath(nextDrive.CurrentLocation, path);
// I'm not a fan of this block of code.
// The goal here is to throw an exception if trying to "CD" into an invalid location
//
// Not sure why the providerInstances are returned as a collection. Feels like given a
// path we should have one provider we're talking to.
if (_providerInstances.ContainsKey(nextDrive.Provider.Name))
{
bool pathExists = false;
IEnumerable<ItemCmdletProvider> cmdletProviders = _providerInstances[nextDrive.Provider.Name].Where(x => x is ItemCmdletProvider).Cast<ItemCmdletProvider>();
ItemCmdletProvider currentProvider = null;
foreach (var provider in cmdletProviders)
{
if (provider.ItemExists(newLocation, providerRuntime))
{
pathExists = true;
currentProvider = provider;
break;
}
}
if (!pathExists)
{
throw new Exception(string.Format("Cannot find path '{0}' because it does not exist.", newLocation));
}
else
{
if (currentProvider is FileSystemProvider)
{
System.Environment.CurrentDirectory = newLocation;
}
}
}
else
{
throw new NotImplementedException("Unsure how to set location with provider:" + nextDrive.Provider.Name);
}
nextDrive.CurrentLocation = newLocation;
CurrentDrive = nextDrive;
_providersCurrentDrive[CurrentDrive.Provider] = CurrentDrive;
return CurrentLocation;
}