Example #1
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 (string.IsNullOrEmpty(fileName))
            {
                return(string.Empty);
            }

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

            var rni        = new UniversalNameInfo();
            int bufferSize = Marshal.SizeOf(rni);

            int intReturn = WNetGetUniversalName(fileName, UniversalNameInfoLevel, ref rni, ref bufferSize);

            if (ErrorMoreData == intReturn)
            {
                IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
                try
                {
                    intReturn = WNetGetUniversalName(fileName, UniversalNameInfoLevel, buffer, ref bufferSize);

                    if (NoError == intReturn)
                    {
                        rni = (UniversalNameInfo)Marshal.PtrToStructure(buffer, typeof(UniversalNameInfo));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }

            switch (intReturn)
            {
            case NoError:
                return(rni.UniversalName);

            case ErrorNotConnected:
                return(string.Empty);

            default:
                return(string.Empty);
            }
        }
Example #2
0
        public string GetDriveMapping(char driveLetter)
        {
            string            localPath = driveLetter.ToString() + ":";
            UniversalNameInfo info      = new UniversalNameInfo();

            info.universalName = "";
            int size = Marshal.SizeOf(info);

            try
            {
                int retVal = WNetGetUniversalName(localPath, UNIVERSAL_NAME_INFO_LEVEL, ref info, ref size);

                if (retVal == 0)
                {
                    return(info.universalName);
                }
                else if (retVal == ERROR_MORE_DATA)
                {
                    IntPtr pBuffer = Marshal.AllocHGlobal(size);
                    try
                    {
                        retVal = WNetGetUniversalName(
                            localPath, UNIVERSAL_NAME_INFO_LEVEL,
                            pBuffer, ref size);

                        if (retVal == NO_ERROR)
                        {
                            info = (UniversalNameInfo)Marshal.PtrToStructure(pBuffer,
                                                                             typeof(UniversalNameInfo));
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(pBuffer);
                    }
                }
                else
                {
                }
            }
            catch
            {
                //MessageBox.Show(e.Message + "\n\n" + e.StackTrace, "Exception in GetDriveInfo()", MOGPromptButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return("");
            }

            return(info.universalName);
        }
 protected static extern int WNetGetUniversalName(string lpLocalPath, int dwInfoLevel, ref UniversalNameInfo lpBuffer, ref int lpBufferSize);
Example #4
0
 protected static extern int WNetGetUniversalName(string lpLocalPath, int dwInfoLevel, ref UniversalNameInfo lpBuffer, ref int lpBufferSize);
Example #5
0
 private static extern int WNetGetUniversalName(string localPath, int infoLevel, ref UniversalNameInfo buffer, ref int bufferSize);
Example #6
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 (string.IsNullOrEmpty(fileName))
            {
                return(string.Empty);
            }

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

            var rni        = new UniversalNameInfo();
            var bufferSize = Marshal.SizeOf(rni);

            var nRet = WNetGetUniversalName(
                fileName, UniversalNameInfoLevel,
                ref rni, ref bufferSize);

            if (ErrorMoreData == nRet)
            {
                var pBuffer = Marshal.AllocHGlobal(bufferSize);

                try
                {
                    nRet = WNetGetUniversalName(
                        fileName, UniversalNameInfoLevel,
                        pBuffer, ref bufferSize);

                    if (NoError == nRet)
                    {
                        rni = (UniversalNameInfo)Marshal.PtrToStructure(pBuffer,
                                                                        typeof(UniversalNameInfo));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(pBuffer);
                }
            }

            switch (nRet)
            {
            case NoError:
                return(rni.lpUniversalName);

            case ErrorNotConnected:
                //Local file-name
                var shi   = LocalShares;
                var share = shi?[fileName];
                if (share == null)
                {
                    return(fileName);
                }
                var path = share.Path;
                if (string.IsNullOrEmpty(path))
                {
                    return(fileName);
                }
                var index = path.Length;
                if (Path.DirectorySeparatorChar != path[path.Length - 1])
                {
                    index++;
                }

                fileName = index < fileName.Length ? fileName.Substring(index) : string.Empty;

                fileName = Path.Combine(share.ToString(), fileName);

                return(fileName);

            default:
                Console.WriteLine("Unknown return value: {0}", nRet);
                return(string.Empty);
            }
        }