Ejemplo n.º 1
0
        public static string GetName(string path_src)
        {
            string unc_path_dest = path_src;             //解決できないエラーが発生した場合は、入力されたパスをそのまま戻す
            int    size          = 1;

            /*
             * 前処理
             *   意図的に、ERROR_MORE_DATAを発生させて、必要なバッファ・サイズ(size)を取得する。
             */
            //1バイトならば、確実にERROR_MORE_DATAが発生するだろうという期待。
            IntPtr lp_dummy = Marshal.AllocCoTaskMem(size);

            //サイズ取得をトライ
            int apiRetVal = WNetGetUniversalName(path_src, UNIVERSAL_NAME_INFO_LEVEL, lp_dummy, ref size);

            //ダミーを解放
            Marshal.FreeCoTaskMem(lp_dummy);


            /*
             * UNC変換処理
             */
            switch (apiRetVal)
            {
            case ERROR_MORE_DATA:
                //受け取ったバッファ・サイズ(size)で再度メモリ確保
                IntPtr lpBufUniversalNameInfo = Marshal.AllocCoTaskMem(size);

                //UNCパスへの変換を実施する。
                apiRetVal = WNetGetUniversalName(path_src, UNIVERSAL_NAME_INFO_LEVEL, lpBufUniversalNameInfo, ref size);

                //UNIVERSAL_NAME_INFOを取り出す。
                UNIVERSAL_NAME_INFO a = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(lpBufUniversalNameInfo, typeof(UNIVERSAL_NAME_INFO));

                //バッファを解放する
                Marshal.FreeCoTaskMem(lpBufUniversalNameInfo);

                if (apiRetVal == NO_ERROR)
                {
                    //UNCに変換したパスを返す
                    unc_path_dest = a.lpUniversalName;
                }
                else
                {
                    //MessageBox.Show(path_src +"ErrorCode:" + apiRetVal.ToString());
                }
                break;

            case ERROR_BAD_DEVICE:                     //すでにUNC名(\\servername\test)
            case ERROR_NOT_CONNECTED:                  //ローカル・ドライブのパス(C:\test)
                //MessageBox.Show(path_src +"\nErrorCode:" + apiRetVal.ToString());
                break;

            default:
                //MessageBox.Show(path_src + "\nErrorCode:" + apiRetVal.ToString());
                break;
            }

            return(unc_path_dest);
        }
Ejemplo n.º 2
0
        protected static string GetUniversalName(string path, int bufferSize)
        {
            IntPtr buffer = IntPtr.Zero;

            try
            {
                buffer = Marshal.AllocHGlobal(bufferSize);

                int oldBufferSize = bufferSize;

                int errorCode =
                    WNetGetUniversalName(path, UNIVERSAL_NAME_INFO_LEVEL, buffer, ref bufferSize);

                if (errorCode == ERROR.SUCCESS)
                {
                    // all clear
                    UNIVERSAL_NAME_INFO uni =
                        (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(buffer, typeof(UNIVERSAL_NAME_INFO));

                    return(uni.lpUniversalName);
                }
                else if (errorCode == ERROR.MORE_DATA)
                {
                    Debug.Assert(bufferSize > oldBufferSize);

                    // more data available....
                    return(GetUniversalName(path, bufferSize));
                }
                else
                {
                    // error occurred... probably invalid path or device went away

                    // Call Debug.Fail on unexpected errors
                    switch (errorCode)
                    {
                    // these two errors are expected
                    case ERROR.BAD_DEVICE:
                    case ERROR.NOT_CONNECTED:
                        break;

                    default:
                        Debug.WriteLine("Encountered error " + errorCode + " while trying to get universal name for " + path);
                        break;
                    }

                    return(null);
                }
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }
        }
Ejemplo n.º 3
0
        //
        //GetUNCPath function
        //
        public static string GetUNCPath(string mappedDrive)
        {
            string uncpath          = mappedDrive;
            UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
            int bufferSize          = Marshal.SizeOf(rni);

            int nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL, ref rni, ref bufferSize);

            if (ERROR_MORE_DATA == nRet)
            {
                IntPtr pBuffer = default(IntPtr);
                try
                {
                    pBuffer = Marshal.AllocHGlobal(bufferSize);
                    nRet    = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL, pBuffer, ref bufferSize);
                    if (NO_ERROR == nRet)
                    {
                        rni     = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer, typeof(UNIVERSAL_NAME_INFO));
                        uncpath = rni.lpUniversalName;
                    }
                    else
                    {
                        uncpath = mappedDrive;
                    }
                }
                finally
                {
                    if (pBuffer != default(IntPtr))
                    {
                        Marshal.FreeHGlobal(pBuffer);
                    }
                }
            }

            System.Uri uri = new Uri(uncpath);
            if (uri.IsUnc)
            {
                System.Net.IPAddress[] ipaddresses = System.Net.Dns.GetHostAddresses(uri.DnsSafeHost);
                if (ipaddresses != null && ipaddresses.Count() > 0)
                {
                    uri = new Uri(string.Format(@"\\{0}\{1}", ipaddresses[0], uri.AbsolutePath));
                }
            }

            return(uri.LocalPath);
        }
Ejemplo n.º 4
0
    //
    //GetUNCPath function
    //
    static public bool TryGetUNCPath(string mappedDrive, out string UNCPath)
    {
        UNCPath = "";

        UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
        int bufferSize          = Marshal.SizeOf(rni);

        int nRet = WNetGetUniversalName(
            mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
            ref rni, ref bufferSize);

        if (ERROR_MORE_DATA == nRet)
        {
            IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
            try
            {
                nRet = WNetGetUniversalName(mappedDrive, 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:
            UNCPath = rni.lpUniversalName;
            return(true);

        case ERROR_NOT_CONNECTED:
            //Local file-name
            return(false);

        default:
            return(false);
        }
    }
Ejemplo n.º 5
0
        //
        //GetUNCPath function
        //
        public static string GetUNCPath(string mappedDrive)
        {
            string uncpath = mappedDrive;
            UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
            int bufferSize = Marshal.SizeOf(rni);

            int nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL, ref rni, ref bufferSize);
            if (ERROR_MORE_DATA == nRet)
            {
                IntPtr pBuffer = default(IntPtr);
                try
                {
                    pBuffer = Marshal.AllocHGlobal(bufferSize);
                    nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL, pBuffer, ref bufferSize);
                    if (NO_ERROR == nRet)
                    {
                        rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer, typeof(UNIVERSAL_NAME_INFO));
                        uncpath = rni.lpUniversalName;
                    }
                    else
                        uncpath = mappedDrive;
                }
                finally
                {
                    if (pBuffer != default(IntPtr))
                        Marshal.FreeHGlobal(pBuffer);
                }
            }

            System.Uri uri = new Uri(uncpath);
            if (uri.IsUnc)
            {
                System.Net.IPAddress[] ipaddresses = System.Net.Dns.GetHostAddresses(uri.DnsSafeHost);
                if (ipaddresses != null && ipaddresses.Count() > 0)
                    uri = new Uri(string.Format(@"\\{0}\{1}", ipaddresses[0], uri.AbsolutePath));
            }

            return uri.LocalPath;
        }
Ejemplo n.º 6
0
        public static string GetUNCPath(string mappedDrive)
        {
            UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
            int bufferSize = Marshal.SizeOf(rni);

            int nRet = WNetGetUniversalName(mappedDrive, UNIVERSAL_NAME_INFO_LEVEL, ref rni, ref bufferSize);

            if (ERROR_MORE_DATA == nRet)
            {
                IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);
                try
                {
                    nRet = WNetGetUniversalName(mappedDrive, 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:
                    return mappedDrive;

                default:
                    return mappedDrive;
            }
        }
Ejemplo n.º 7
0
 protected static extern int WNetGetUniversalName(string lpLocalPath,
                                                  int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
Ejemplo n.º 8
0
		private static extern int WNetGetUniversalName (string lpLocalPath,
			int dwInfoLevel,
			ref UNIVERSAL_NAME_INFO lpBuffer,
			ref int lpBufferSize );
Ejemplo n.º 9
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);
            }
        }
Ejemplo n.º 10
0
        public static string GetUncPathForLocalPath(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return String.Empty;
            }
            if (fileName.StartsWith(@"\\"))
            {
                return fileName;
            }

            fileName = Path.GetFullPath(fileName);
            if (!IsValidLocalFilePath(fileName) || !Directory.Exists(fileName))
            {
                return String.Empty;
            }

            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 (nRet == ERROR_MORE_DATA)
            {
                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
                    List<ShareInfo> shareInfoList = GetShareInfo();
                    if (shareInfoList != null)
                    {
                        ShareInfo shareInfo = FindBestShareInfoMatch(shareInfoList, fileName);
                        if (shareInfo != null)
                        {
                            // TODO : (Steph) verify what this ugly code is doing ...
                            string path = shareInfo.Path;
                            if (!String.IsNullOrEmpty(path))
                            {
                                int index = path.Length;
                                if (Path.DirectorySeparatorChar != path[path.Length - 1])
                                {
                                    index++;
                                }

                                if (index < fileName.Length)
                                {
                                    fileName = fileName.Substring(index);
                                }
                                else
                                {
                                    fileName = String.Empty;
                                }
                                return Path.Combine(shareInfo.ToString(), fileName);
                            }
                        }
                    }
                    return String.Empty;

                default:
                    return String.Empty;
            }
        }
Ejemplo n.º 11
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
          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;
      }
    }
Ejemplo n.º 12
0
        public static string GetUncPathForLocalPath(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                return(String.Empty);
            }
            if (fileName.StartsWith(@"\\"))
            {
                return(fileName);
            }

            fileName = Path.GetFullPath(fileName);
            if (!IsValidLocalFilePath(fileName) || !Directory.Exists(fileName))
            {
                return(String.Empty);
            }

            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 (nRet == ERROR_MORE_DATA)
            {
                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
                List <ShareInfo> shareInfoList = GetShareInfo();
                if (shareInfoList != null)
                {
                    ShareInfo shareInfo = FindBestShareInfoMatch(shareInfoList, fileName);
                    if (shareInfo != null)
                    {
                        // TODO : (Steph) verify what this ugly code is doing ...
                        string path = shareInfo.Path;
                        if (!String.IsNullOrEmpty(path))
                        {
                            int index = path.Length;
                            if (Path.DirectorySeparatorChar != path[path.Length - 1])
                            {
                                index++;
                            }

                            if (index < fileName.Length)
                            {
                                fileName = fileName.Substring(index);
                            }
                            else
                            {
                                fileName = String.Empty;
                            }
                            return(Path.Combine(shareInfo.ToString(), fileName));
                        }
                    }
                }
                return(String.Empty);

            default:
                return(String.Empty);
            }
        }