public static class Program
const string ShareExtUNCFullPrefix = @"\\?\UNC\";
const string ShareExtPrefix = @"\\?\";
const string SharePrefix = @"\\";
const string DIRDOUBLE = @"\\";
const string COLONDIR = @":\";
const string COLON = @":";
const string DIRSPACE = @"\ ";
const string SPACEDIR = @" \";
const string SPACE = @" ";
public static int IndexOfOccurence(this string s, char match, int occurence)
while (i <= occurence && (index = s.IndexOf(match, index + 1)) != -1)
public static string GetUNCHostIPAddressShare(this string UNCFullPath)
if (UNCFullPath.StartsWith(DIRDOUBLE))
if (p.StartsWith(ShareExtPrefix))
p = p.Substring(3, p.Length - 3);
if (p.StartsWith(@"\UNC\"))
p = DIRDOUBLE + p.Substring(5, p.Length - 5);
int idx3rd = (p.Length >= 5 ) ? p.IndexOf(cDIR, 3) : -1;
if (p[0]==cDIR && p[1]==cDIR && Char.IsLetterOrDigit(p[2]) && p.Length >= 5 &&idx3rd > -1 && Char.IsLetter(p[idx3rd+1]) && p.IndexOf(cDIR, idx3rd + 2) > -1)
int i = p.IndexOfOccurence(cDIR, 4);
return p.Substring(0, i);
public static void Main()
Console.WriteLine(@"\\2001:4860:4860::8888\share\tail\dir1".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\8.8.8.8\share\tail\dir1".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\host\share\tail\dir1".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\host\share\tail\file.txt".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\\?\UNC\share\tail\file.txt".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\h\s\file.txt".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\h\s\".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\\s\".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\h\\".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\\\\\\\".GetUNCHostIPAddressShare());
Console.WriteLine(@"\\?\UNC\share\tail\file.txt".GetUNCHostIPAddressShare() + ", 1 failed case - UNC is Server Name");