Example #1
0
        /// <summary>
        /// Returns the <see cref="Share"/> which matches a given local path
        /// </summary>
        /// <param name="path">The path to match</param>
        public SambaShare this[string path]
        {
            get
            {
                if (null == path || 0 == path.Length)
                {
                    return(null);
                }

                path = Path.GetFullPath(path);
                if (!IsValidFilePath(path))
                {
                    return(null);
                }

                SambaShare match = null;

                for (int i = 0; i < InnerList.Count; i++)
                {
                    SambaShare s = (SambaShare)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);
            }
        }
Example #2
0
 protected void Add(SambaShare share)
 {
     InnerList.Add(share);
 }
Example #3
0
 /// <summary>
 /// Copy this collection to an array
 /// </summary>
 /// <param name="array"></param>
 /// <param name="index"></param>
 public void CopyTo(SambaShare[] array, int index)
 {
     InnerList.CopyTo(array, index);
 }
Example #4
0
        /// <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
                ShareCollection shi = LocalShares;
                if (null != shi)
                {
                    SambaShare 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:
                return(string.Empty);
            }
        }
Example #5
0
 protected void Add(SambaShare share)
 {
     InnerList.Add(share);
 }