public static extern int WNetGetUniversalName(string lpLocalPath, int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
public static extern int WNetGetUniversalName(string lpLocalPath, int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
/// <summary> /// Returns the UNC path for a mapped drive or local share. /// </summary> /// <param name="fileName">The path to map</param> /// <returns>The UNC path (if available)</returns> public static string PathToUnc(string fileName) { if (null == fileName || 0 == fileName.Length) return string.Empty; fileName = Path.GetFullPath(fileName); if (!IsValidFilePath(fileName)) return fileName; int nRet = 0; UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO(); int bufferSize = Marshal.SizeOf(rni); nRet = UncUtilities.WNetGetUniversalName( fileName, UncUtilities.UNIVERSAL_NAME_INFO_LEVEL, ref rni, ref bufferSize); if (UncUtilities.ERROR_MORE_DATA == nRet) { IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize); ; try { nRet = UncUtilities.WNetGetUniversalName( fileName, UncUtilities.UNIVERSAL_NAME_INFO_LEVEL, pBuffer, ref bufferSize); if (UncUtilities.NO_ERROR == nRet) { rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer, typeof(UNIVERSAL_NAME_INFO)); } } finally { Marshal.FreeHGlobal(pBuffer); } } switch (nRet) { case UncUtilities.NO_ERROR: return rni.lpUniversalName; case UncUtilities.ERROR_NOT_CONNECTED: //Local file-name ShareCollection shi = LocalShares; if (null != shi) { Share share = shi[fileName]; if (null != share) { string path = share.Path; if (null != path && 0 != path.Length) { int index = path.Length; if (Path.DirectorySeparatorChar != path[path.Length - 1]) index++; if (index < fileName.Length) fileName = fileName.Substring(index); else fileName = string.Empty; fileName = Path.Combine(share.ToString(), fileName); } } } return fileName; default: Console.WriteLine("Unknown return value: {0}", nRet); return string.Empty; } }