/// <summary> /// Returns the <see cref="Share"/> which matches a given local path /// </summary> /// <param name="path">The path to match</param> public NetShare this[string path] { get { if (null == path || 0 == path.Length) { return(null); } path = Path.GetFullPath(path); if (!IsValidFilePath(path)) { return(null); } NetShare match = null; for (int i = 0; i < InnerList.Count; i++) { NetShare s = (NetShare)InnerList[i]; if (s.IsFileSystem && s.MatchesPath(path)) { //Store first match if (null == match) { match = s; } // If this has a longer path, // and this is a disk share or match is a special share, // then this is a better match else if (match.Path.Length < s.Path.Length) { if (ShareType.Disk == s.ShareType || ShareType.Disk != match.ShareType) { match = s; } } } } return(match); } }
protected void Add(NetShare share) { InnerList.Add(share); }
/// <summary> /// Copy this collection to an array /// </summary> /// <param name="array"></param> /// <param name="index"></param> public void CopyTo(NetShare[] array, int index) { InnerList.CopyTo(array, index); }
/// <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 = WNetGetUniversalName( fileName, UNIVERSAL_NAME_INFO_LEVEL, ref rni, ref bufferSize); if (ERROR_MORE_DATA == nRet) { IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);; try { nRet = WNetGetUniversalName( fileName, UNIVERSAL_NAME_INFO_LEVEL, pBuffer, ref bufferSize); if (NO_ERROR == nRet) { rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer, typeof(UNIVERSAL_NAME_INFO)); } } finally { Marshal.FreeHGlobal(pBuffer); } } switch (nRet) { case NO_ERROR: return(rni.lpUniversalName); case ERROR_NOT_CONNECTED: //Local file-name NetShareCollection shi = LocalNetShares; if (null != shi) { NetShare 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); } }