VC--修改收藏夹Favorite路径
也就是读写注册表。
重装系统总是忘记备份收藏夹,很郁闷。查了一下更改默认收藏夹的方法为修改注册表,将
HKEY_USERS\.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders改为你想要的路径即可。
决定用程序实现。代码异常简单。放上来作为RegOpenKeyEx的例子程序参考备用。
该段代码根据一个开关,决定是否将旧的Favorites拷贝到新的路径中。
1
CString strDisp;
2 TCHAR szDir[MAX_PATH] = { 0 }; // 要设定的Path
3 TCHAR szOldPath[MAX_PATH] = { 0 };
4
5 GetDlgItemText(IDC_EDIT_PATH,szDir,MAX_PATH);
6
7 BOOL bDir = PathIsDirectory(szDir);
8
9 if (bDir == FALSE)
10 {
11 strDisp.Format(_T( " Please Select a Folder. " ));
12 SetDlgItemText(IDC_EDIT_PATH,strDisp);
13 return ;
14 }
15
16 // Open Reg
17 HKEY hKey;
18 LONG lRtn = RegOpenKeyEx(HKEY_CURRENT_USER ,
19 _T( " Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders " ),
20 0 , KEY_ALL_ACCESS,
21 & hKey);
22
23 if (lRtn == ERROR_SUCCESS)
24 {
25 // Read old key
26 DWORD dwOldLen = MAX_PATH;
27 // DWORD dwReserved;
28 DWORD dwType = 0 ;
29 lRtn = RegQueryValueEx(hKey,_T( " Favorites " ),NULL, & dwType,(BYTE * )szOldPath, & dwOldLen);
30
31 // Write key
32 DWORD dwLen = (DWORD)(_tcslen(szDir) + 1 ) * sizeof (TCHAR);
33 lRtn = RegSetValueEx(hKey,_T( " Favorites " ), 0L ,REG_EXPAND_SZ, ( const BYTE * )szDir, dwLen);
34
35 // Close Key
36 RegCloseKey(hKey);
37 }
38
39 int nChecked = m_Check.GetCheck();
40 if (nChecked)
41 {
42 TCHAR szExpandBuf[MAX_PATH] = { 0 };
43 ExpandEnvironmentStrings(
44 szOldPath, // szExpandBuf, //
45 MAX_PATH //
46 );
47
48 CopyDirectory(szExpandBuf, szDir);
49 }
50
51 if (lRtn == ERROR_SUCCESS)
52 {
53 strDisp.Format(_T( " Successful!! Favorite = %s " ), szDir);
54 SetDlgItemText(IDC_EDIT_PATH,strDisp);
55 }
56
2 TCHAR szDir[MAX_PATH] = { 0 }; // 要设定的Path
3 TCHAR szOldPath[MAX_PATH] = { 0 };
4
5 GetDlgItemText(IDC_EDIT_PATH,szDir,MAX_PATH);
6
7 BOOL bDir = PathIsDirectory(szDir);
8
9 if (bDir == FALSE)
10 {
11 strDisp.Format(_T( " Please Select a Folder. " ));
12 SetDlgItemText(IDC_EDIT_PATH,strDisp);
13 return ;
14 }
15
16 // Open Reg
17 HKEY hKey;
18 LONG lRtn = RegOpenKeyEx(HKEY_CURRENT_USER ,
19 _T( " Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders " ),
20 0 , KEY_ALL_ACCESS,
21 & hKey);
22
23 if (lRtn == ERROR_SUCCESS)
24 {
25 // Read old key
26 DWORD dwOldLen = MAX_PATH;
27 // DWORD dwReserved;
28 DWORD dwType = 0 ;
29 lRtn = RegQueryValueEx(hKey,_T( " Favorites " ),NULL, & dwType,(BYTE * )szOldPath, & dwOldLen);
30
31 // Write key
32 DWORD dwLen = (DWORD)(_tcslen(szDir) + 1 ) * sizeof (TCHAR);
33 lRtn = RegSetValueEx(hKey,_T( " Favorites " ), 0L ,REG_EXPAND_SZ, ( const BYTE * )szDir, dwLen);
34
35 // Close Key
36 RegCloseKey(hKey);
37 }
38
39 int nChecked = m_Check.GetCheck();
40 if (nChecked)
41 {
42 TCHAR szExpandBuf[MAX_PATH] = { 0 };
43 ExpandEnvironmentStrings(
44 szOldPath, // szExpandBuf, //
45 MAX_PATH //
46 );
47
48 CopyDirectory(szExpandBuf, szDir);
49 }
50
51 if (lRtn == ERROR_SUCCESS)
52 {
53 strDisp.Format(_T( " Successful!! Favorite = %s " ), szDir);
54 SetDlgItemText(IDC_EDIT_PATH,strDisp);
55 }
56
文件夹递归拷贝代码,实在懒的填注释了,好在代码不长,能读就成:
BOOL CopyDirectory(LPCTSTR lpszSrcPath, LPCTSTR lpszDstPath)
{
BOOL bCopyRet = FALSE;
BOOL bRet;
WIN32_FIND_DATA FindData;
HANDLE hFindFile;
TCHAR szFileSrc[MAX_PATH] = { 0 };
TCHAR szFileDst[MAX_PATH] = { 0 };
TCHAR szFilter[MAX_PATH] = { 0 };
TCHAR * pszSrcPath = NULL;
TCHAR * pszDstPath = NULL;
if ( ! lpszSrcPath ||! lpszDstPath)
return FALSE;
_tcscpy(szFileDst, lpszDstPath);
int nLen = ( int )_tcslen(szFileDst);
if (szFileDst[nLen - 1 ] != _T( ' \\ ' ))
{
CreateDirectory(szFileDst,NULL);
szFileDst[nLen] = _T( ' \\ ' );
szFileDst[nLen + 1 ] = _T( ' \0 ' );
}
_tcscpy(szFileSrc, lpszSrcPath);
_tcscpy(szFilter, lpszSrcPath);
nLen = ( int )_tcslen(szFileSrc);
if (szFileSrc[nLen - 1 ] != _T( ' \\ ' ))
{
_tcscat(szFilter, _T( " \\*.* " ));
szFileSrc[nLen] = _T( ' \\ ' );
szFileSrc[nLen + 1 ] = _T( ' \0 ' );
}
else
{
_tcscat(szFilter, _T( " \\*.* " ));
}
pszSrcPath = szFileSrc + _tcslen(szFileSrc);
pszDstPath = szFileDst + _tcslen(szFileDst);
hFindFile = FindFirstFile(szFilter, & FindData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if (_tcscmp(FindData.cFileName,_T( " . " )) && _tcscmp(FindData.cFileName,_T( " .. " )) &&
! (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
_tcscpy(pszSrcPath,FindData.cFileName);
_tcscpy(pszDstPath,FindData.cFileName);
bCopyRet = CopyFile(szFileSrc, szFileDst, FALSE);
}
if (_tcscmp(FindData.cFileName,_T( " . " )) && _tcscmp(FindData.cFileName,_T( " .. " )) &&
FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tcscpy(pszSrcPath,FindData.cFileName);
_tcscpy(pszDstPath,FindData.cFileName);
CopyDirectory(szFileSrc, szFileDst);
}
bRet = FindNextFile(hFindFile, & FindData);
} while (bRet);
FindClose(hFindFile);
}
return TRUE;
}
{
BOOL bCopyRet = FALSE;
BOOL bRet;
WIN32_FIND_DATA FindData;
HANDLE hFindFile;
TCHAR szFileSrc[MAX_PATH] = { 0 };
TCHAR szFileDst[MAX_PATH] = { 0 };
TCHAR szFilter[MAX_PATH] = { 0 };
TCHAR * pszSrcPath = NULL;
TCHAR * pszDstPath = NULL;
if ( ! lpszSrcPath ||! lpszDstPath)
return FALSE;
_tcscpy(szFileDst, lpszDstPath);
int nLen = ( int )_tcslen(szFileDst);
if (szFileDst[nLen - 1 ] != _T( ' \\ ' ))
{
CreateDirectory(szFileDst,NULL);
szFileDst[nLen] = _T( ' \\ ' );
szFileDst[nLen + 1 ] = _T( ' \0 ' );
}
_tcscpy(szFileSrc, lpszSrcPath);
_tcscpy(szFilter, lpszSrcPath);
nLen = ( int )_tcslen(szFileSrc);
if (szFileSrc[nLen - 1 ] != _T( ' \\ ' ))
{
_tcscat(szFilter, _T( " \\*.* " ));
szFileSrc[nLen] = _T( ' \\ ' );
szFileSrc[nLen + 1 ] = _T( ' \0 ' );
}
else
{
_tcscat(szFilter, _T( " \\*.* " ));
}
pszSrcPath = szFileSrc + _tcslen(szFileSrc);
pszDstPath = szFileDst + _tcslen(szFileDst);
hFindFile = FindFirstFile(szFilter, & FindData);
if (hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if (_tcscmp(FindData.cFileName,_T( " . " )) && _tcscmp(FindData.cFileName,_T( " .. " )) &&
! (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
_tcscpy(pszSrcPath,FindData.cFileName);
_tcscpy(pszDstPath,FindData.cFileName);
bCopyRet = CopyFile(szFileSrc, szFileDst, FALSE);
}
if (_tcscmp(FindData.cFileName,_T( " . " )) && _tcscmp(FindData.cFileName,_T( " .. " )) &&
FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tcscpy(pszSrcPath,FindData.cFileName);
_tcscpy(pszDstPath,FindData.cFileName);
CopyDirectory(szFileSrc, szFileDst);
}
bRet = FindNextFile(hFindFile, & FindData);
} while (bRet);
FindClose(hFindFile);
}
return TRUE;
}