比如我想获取的url连接文件路径是: C:\Documents and Settings\Administrator\Favorites\Msn.url我怎么通过编程来打开这个连接文件,我是这样做的:CString myUrl=_T(""); //这个是连接文件中的url指向地址
CString filename = _T("C:\\Documents and Settings\\Administrator\\Favorites\\Msn.url");
CStdioFile  urlfile; 
urlfile.Open(filename,CFile::modeRead ,NULL); 
while(urlfile.ReadString(line))
{   
     if(line.Left(4).CompareNoCase(_T("URL="))==0 ) 
     {   
line.Delete(0,4);   
myUrl=line; 
break;
     }  
}  
urlfile.Close(); 
但是运行的时候 ,在对文件进行open的时候就出错,难道.url文件不能这样打开?
应该怎么来获取.url的内容?

解决方案 »

  1.   

    把这个.URL文件当成.Ini文件读取,
    [code=BatchFile]
    [DEFAULT]
    BASEURL=Protocol://URL
    [InternetShortcut]
    URL=Protocol://URL
    Modified=
    IconFile=Protocol://URL.ico
    IconIndex=IconIndex
    [/code]
      

  2.   


    TCHAR szUrl[1024] = "";
    GetPrivateProfileString(_T("InternetShortcut"), _T("URL"), NULL, szUrl, 1024, _T("d:\\abc\def.url"));ShellExecute(NULL, _T("open"), szUrl, ...);
      

  3.   

    按照1楼的说法试一下,不行的话把.url文件改名字(用API)再读取..
    再不行就用IShellUIHelper..
    再不行你复制下面的代码吧!! //生成“我的收藏夹”子菜单
    TCHAR           sz[MAX_PATH];
    TCHAR           szPath[MAX_PATH];
    HKEY            hKey;
    DWORD           dwSize;
    CMenu*          pMenu; // 开始消去存在的菜单
    pMenu = GetMenu()->GetSubMenu(4);
    while(pMenu->DeleteMenu(0, MF_BYPOSITION)); //从注册表中找到“我的收藏夹的位置”,然后读写注册表
    if(RegOpenKey(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders"), &hKey) != ERROR_SUCCESS)
    {
    TRACE0("Favorites folder not found\n");
    return 0;
    }
    dwSize = sizeof(sz);
    RegQueryValueEx(hKey, _T("Favorites"), NULL, NULL, (LPBYTE)sz, &dwSize);
    ExpandEnvironmentStrings(sz, szPath, MAX_PATH);
    RegCloseKey(hKey);
        //添加我的收藏菜单项
    BuildFavoritesMenu(szPath, 0, pMenu);
    return 0;
    BuildFavoritesMenu(LPCTSTR pszPath, int nStartPos, CMenu *pMenu)
    {
    CString         strPath(pszPath);
    CString         strPath2;
    CString         str;
    WIN32_FIND_DATA wfd;
    HANDLE          h;
    int             nPos;
    int             nEndPos;
    int             nNewEndPos;
    int             nLastDir;
    TCHAR           buf[INTERNET_MAX_PATH_LENGTH];
    CStringArray    astrFavorites;
    CStringArray    astrDirs;
    CMenu*          pSubMenu; // 确定有'\\'字符
    if(strPath[strPath.GetLength() - 1] != _T('\\'))
    strPath += _T('\\');
    strPath2 = strPath;
    strPath += "*.*"; // 首先扫描 .URL 然后扫描子目录
    h = FindFirstFile(strPath, &wfd);
    if(h != INVALID_HANDLE_VALUE)
    {
    nEndPos = nStartPos;
    do
    {
    if((wfd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM))==0)
    {
    str = wfd.cFileName;
    if(str.Right(4) == _T(".url"))
    {
    // an .URL file is formatted just like an .INI file, so we can
    // use GetPrivateProfileString() to get the information we want
    ::GetPrivateProfileString(_T("InternetShortcut"), _T("URL"),
      _T(""), buf, INTERNET_MAX_PATH_LENGTH,
      strPath2 + str);
    str = str.Left(str.GetLength() - 4); // scan through the array and perform an insertion sort
    // to make sure the menu ends up in alphabetic order
    for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
    {
    if(str.CompareNoCase(astrFavorites[nPos]) < 0)
    break;
    }
    astrFavorites.InsertAt(nPos, str);
    m_astrFavoriteURLs.InsertAt(nPos, buf);
    ++nEndPos;
    }
    }
    } while(FindNextFile(h, &wfd));
    FindClose(h);
    // Now add these items to the menu
    for(nPos = nStartPos ; nPos < nEndPos ; ++nPos)
    {
    pMenu->AppendMenu(MF_STRING | MF_ENABLED, 0xe00 + nPos, astrFavorites[nPos]);
    }
    // now that we've got all the .URL files, check the subdirectories for more
    nLastDir = 0;
    h = FindFirstFile(strPath, &wfd);
    ASSERT(h != INVALID_HANDLE_VALUE);
    do
    {
    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    // ignore the current and parent directory entries
    if(lstrcmp(wfd.cFileName, _T(".")) == 0 || lstrcmp(wfd.cFileName, _T("..")) == 0)
    continue; for(nPos = 0 ; nPos < nLastDir ; ++nPos)
    {
    if(astrDirs[nPos].CompareNoCase(wfd.cFileName) > 0)
    break;
    }
    pSubMenu = new CMenu;
    pSubMenu->CreatePopupMenu(); // call this function recursively.
    nNewEndPos = BuildFavoritesMenu(strPath2 + wfd.cFileName, nEndPos, pSubMenu);
    if(nNewEndPos != nEndPos)
    {
    // only intert a submenu if there are in fact .URL files in the subdirectory
    nEndPos = nNewEndPos;
    pMenu->InsertMenu(nPos, MF_BYPOSITION | MF_POPUP | MF_STRING, (UINT)pSubMenu->m_hMenu, wfd.cFileName);
    pSubMenu->Detach();
    astrDirs.InsertAt(nPos, wfd.cFileName);
    ++nLastDir;
    }
    delete pSubMenu;
    }
    } while(FindNextFile(h, &wfd));
    FindClose(h);
    }
    return nEndPos;
    }
      

  4.   

    最正确的方法是使用IUniformResourceLocator,这个接口不在MSDN文档中,但是在 intshcut.h 文件中定义了。
    使用CLSID_InternetShortcut创建一个Internet Shortcut对象实例,并得到IUniformResourceLocator接口对象指针。 
    查询得到一个IPersistFile接口指针。 
    使用IPersistFile::Load函数,以.url文件名为参数,初始化Shortcut对象。
    用IUniformResourceLocator::GetURL函数即可得到URL
      

  5.   

    http://support.microsoft.com/kb/229092/zh-cn