在測試我們的應用程序,我們發現,使用的SHGetFolderPath返回AppData的路徑該函數返回即使測試PC上存在的文件夾爲零。在開發PC上,ShGetFolderPath返回沒有錯誤的AppData路徑。爲什麼的SHGetFolderPath返回nil一些Vista電腦的
開發PC和測試PC運行Vista。
function GetShellFolder(ID: Cardinal; Create: Boolean = False): string;
// This function is a superset of SHGetSpecialFolderPath, included with
// earlier versions of the Shell. On systems preceeding those including
// Shell32.dll version 5.0 (Windows Millennium Edition (Windows Me) and
// Windows 2000), SHGetFolderPath was obtained through SHFolder.dll,
// distributed with Microsoft Internet Explorer 4.0 and later versions.
// Takes the CSIDL of a folder and returns the path or 'Could not determine
// folder path' if it does not exist. Creates the folder if it does not
// exist if Create is true.
var
Res: HResult;
Path: array [ 0 .. Max_Path ] of Char;
begin
if Create then
ID := ID or csidl_Flag_Create;
Res := ShGetFolderPath(0, ID, 0, shgfp_Type_Current, Path);
if S_OK <> Res then
begin
Result := 'Could not determine folder path';
raise Exception.Create('Could not determine folder path');
end;
Result := Path;
end;
GetShellFolder(CSIDL_LOCAL_APPDATA, False);
在開發機成功返回CSIDL_LOCAL_APPDATA路徑,但在測試電腦上沒有返回CSIDL_LOCAL_APPDATA文件夾。
有誰知道爲什麼即使在硬盤驅動器上的文件夾的測試電腦上沒有返回CSIDL_LOCAL_APPDATA文件夾?測試機器返回歷史文件夾與CSIDL_HISTORY,但它不會返回與CSIDL_LOCAL_APPDATA本地appdata文件夾。
在測試PC資源管理器顯示CSIDL_LOCAL_APPDATA文件夾的用戶\用戶\應用程序數據\地方。在測試中,PC瀏覽器將CSIDL_HISTORY文件夾顯示爲user \ user \ AppData \ Local \ Microsoft \ Windows \ History。
如果我們調用GetShellFolder(CSIDL_LOCAL_APPDATA,真)的功能仍然沒有恢復的文件夾路徑。
我在做什麼錯,或者我該如何解決這個問題?
您的功能不會「返回」「無法確定文件夾路徑」。當函數引發異常時,根本不存在返回值*。你正在檢查'S_OK'的結果,但是你沒有考慮其他可能的返回值,例如'S_False','E_Fail'和'E_InvalidArg'。當ShGetFolderPath失敗時,它應該告訴你爲什麼。不要忽視這些信息。還有一件事:你的程序是一項服務,還是以提升的權限運行? – 2010-02-09 15:20:29
這是一個沒有提升權限的普通應用程序。 – Bill 2010-02-09 15:38:34
爲什麼硬編碼錯誤代碼而不是使用RaiseLastOSError?您將獲得更有用的本地化錯誤消息以及錯誤代碼 – 2010-02-09 20:34:53