예제 #1
0
 protected void Add(Share share)
 {
     InnerList.Add(share);
 }
예제 #2
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)
                {
                    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);
            }
        }