如何添加链接到手机收藏夹

来源:互联网 发布:重庆时时彩二星软件 编辑:程序博客网 时间:2024/04/29 01:26

在收藏夹里的链接都是以*.url存储在/Storage/Windows/Favorites目录中。所以要添加一个新的链接,必须要做两件事。

1.得到收藏夹所在目录

2.在此目录中创建一个新的*.url文件

得到目录名可以用SHGetSpecialFolderPath使用CSIDL_FAVORITES flag。至于*.url文件,以下是一个简单的ANSI文本文件内容

[InternetShortcut]
URL=<URL assigned to Favorites link>
以下为添加,删除链接的示例代码
BOOL MakeLinkFileName(LPCTSTR pszName, LPTSTR pszBuf){
        // Get path to Favorites folder.
        if(!SHGetSpecialFolderPath(NULL, pszBuf, CSIDL_FAVORITES, TRUE))return FALSE;
        // Make file name.
        _tcscat(pszBuf, _T("//"));
        _tcscat(pszBuf, pszName);
        _tcscat(pszBuf, _T(".url"));
        return TRUE;
}
 
BOOL AddLinkToFavorites(LPCTSTR pszName, LPCTSTR pszURL, BOOL bFailIfExists)
{
         // Get link file name.
         TCHAR szFileName[MAX_PATH];
         if(!MakeLinkFileName(pszName, szFileName))
            return FALSE;
         if(GetFileAttributes(szFileName) != 0xFFFFFFFF && bFailIfExists)
         {
                 // File exists. Do not overwrite it.
                 return FALSE;
}
         DeleteFile(szFileName);
 
         // Create *.url file.
         FILE *pFile = _tfopen(szFileName, _T("wt"));
         if(pFile == NULL)
           return FALSE;
         _fputts(_T("[InternetShortcut]/n"), pFile);
         TCHAR *pBuf = (TCHAR*)_alloca(10+_tcslen(pszURL));
         _stprintf(pBuf, _T("URL=%s"), pszURL);
         _fputts(pBuf, pFile);
         fclose(pFile);
 
         return TRUE;
}
BOOL RemoveLinkFromFavorites(LPCTSTR pszName)
{
// Get link file name.
         TCHAR szFileName[MAX_PATH];
         if(!MakeLinkFileName(pszName, szFileName))
             return FALSE;
          
         return DeleteFile(szFileName);
}
 
 
The following code adds "Spb Software House" link to Favorites and then removes it. 
 
// Add link.
 
AddLinkToFavorites(_T("Spb Software House"), _T("http://www.spbsoftwarehouse.com"), FALSE);
...
 
// Remove link.
 
RemoveLinkFromFavorites(_T("Spb Software House"));
原创粉丝点击