Created
April 25, 2016 05:35
-
-
Save mattjcowan/b61c96b176fbcb2607de1c5f0d2f5b55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool AreIdenticalPaths(DirectoryInfo dir1, DirectoryInfo dir2) | |
{ | |
return string.Compare( | |
dir1.FullName.TrimEnd('/', '\\'), | |
dir2.FullName.TrimEnd('/', '\\'), | |
StringComparison.InvariantCultureIgnoreCase) == 0; | |
} | |
public static string MapPath(string virtualPath) | |
{ | |
return HostingEnvironment.IsHosted | |
? HostingEnvironment.MapPath(virtualPath) | |
: CombinePaths(GetAppDomainDirectory(), virtualPath); | |
} | |
/* | |
* | |
var x = new List<string[]> { | |
new[] { "~/", "", "abc", @"\def", @"def\/", "def//\\/" }, | |
new[] { "x", @"~\xy", @"\g/" }, | |
new[] { "\\x", @"~\xy", @"\g" }, | |
new[] { "//x", @"~\xy", @"\g/" }, | |
new[] { @"\\documents\", @"~\xy", @"\\g/" }, | |
new[] { "~/" }, | |
}; | |
foreach (var y in x) | |
{ | |
Console.WriteLine(CombinePaths(y)); | |
} | |
// produces | |
\abc\def\def\def\ | |
x\xy\g\ | |
\x\xy\g | |
\\x\xy\g\ | |
\\documents\xy\g\ | |
\ | |
*/ | |
public static string CombinePaths(params string[] paths) | |
{ | |
if (paths == null || paths.Length == 0) | |
return string.Empty; | |
var first = paths[0].Replace("/", @"\").Trim(); | |
var last = paths[paths.Length - 1].Replace("/", @"\").Trim(); | |
var path = string.Join(@"\", paths.Select(p => p.Trim().Trim('~', '/', '\\')).ToArray()); | |
while (path.Contains(@"\\")) | |
path = path.Replace(@"\\", @"\"); | |
while (first.StartsWith(@"\")) | |
{ | |
first = first.Substring(1); | |
path = @"\" + path; | |
} | |
if (last.EndsWith(@"\")) | |
path += @"\"; | |
return NormalizeDirectorySeparatorChars(path); | |
} | |
public static string NormalizeDirectorySeparatorChars(string path) | |
{ | |
return Path.DirectorySeparatorChar == '/' ? path.Replace('\\', '/') : path.Replace('/', '\\'); | |
} | |
public static string GetMachineName() | |
{ | |
string machineName; | |
try | |
{ | |
machineName = Environment.MachineName; | |
} | |
catch | |
{ | |
try | |
{ | |
machineName = Dns.GetHostName(); | |
} | |
catch | |
{ | |
throw new ApplicationException("Unable to retrieve the machine name"); | |
} | |
} | |
return machineName.ToAlphaNumeric('-', '-'); | |
} | |
public static AppDomain GetAppDomain() | |
{ | |
return AppDomain.CurrentDomain; | |
} | |
public static string GetAppDomainDirectory() | |
{ | |
//return Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath) | |
return GetAppDomain().BaseDirectory; | |
} | |
public static string GetAppVirtualPath() | |
{ | |
return HostingEnvironment.IsHosted ? HostingEnvironment.ApplicationVirtualPath : "/"; | |
} | |
public static string GetAppPhysicalPath() | |
{ | |
return MapPath("~/"); | |
} | |
public static string GetAppBinDirectory() | |
{ | |
return HostingEnvironment.IsHosted ? HttpRuntime.BinDirectory : | |
GetAppDomainDirectory(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment