public static string GetCurrentDirectory(bool expandTilde = false) { var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile).ToLowerInvariant(); var current = ShellUtility.GetProperDirectoryCapitalization(Directory.GetCurrentDirectory()); return(!expandTilde && current.ToLowerInvariant().StartsWith(home) ? $"~{current.Substring( home.Length )}" : current); }
public static string ProcessTilde(string path) { if (!path.StartsWith("~")) { return(ShellUtility.GetProperDirectoryCapitalization(path)); } var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var withoutTilde = path.Substring(1) .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar) .Trim(Path.DirectorySeparatorChar) .Split( new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries ); var newPath = Path.Combine(withoutTilde.Prepend(home).ToArray()); return(ShellUtility.GetProperDirectoryCapitalization(newPath)); }
public static string GetProperFilePathCapitalization(string fileName) { try { if (!File.Exists(fileName)) { return(fileName); } var fileInfo = new FileInfo(fileName); var dirInfo = fileInfo.Directory; return(dirInfo is null ? fileInfo.FullName : Path.Combine( ShellUtility.GetProperDirectoryCapitalization(dirInfo.FullName), dirInfo.GetFiles(fileInfo.Name)[0].Name )); } catch { return(fileName); } }
public static string GetProperDirectoryCapitalization(string dir) { try { if (!Directory.Exists(dir)) { return(dir); } var dirInfo = new DirectoryInfo(dir); var parentDirInfo = dirInfo.Parent; return(parentDirInfo is null ? dirInfo.Name : Path.Combine( ShellUtility.GetProperDirectoryCapitalization(parentDirInfo.FullName), parentDirInfo.GetDirectories(dirInfo.Name)[0].Name )); } catch { return(dir); } }