我正在使用Delphi 2010,我的程序想要獲得系統的臨時路徑。我正在使用TPath.GetTempPath,一切工作正常......至少對我和我的同事來說。但在某些客戶機器上,此方法返回(當然)不存在的裁剪路徑。我發現問題似乎是對GetLongPathName()的底層調用的結果。Delphi TPath.GetTempPath結果被裁剪
完整的代碼如下所示:
[...]
var
TmpDir : String;
Len : Integer;
begin
[... Call to GetTempPath succeeds and we have a valid temp directory in short "~" notation in var TmpDir ...]
Len := GetLongPathName(PChar(TmpDir), nil, 0); // Len = 37
SetLength(TmpDir, Len - 1); // We want to set the len of TmpDir to 37 - 1.
GetLongPathName(PChar(TmpDir), PChar(TmpDir), Len); // Only 32 (instead of 36) characters are copied - so we have a cropped path - But why?!
end;
[...]
這只是發生在某些系統上,我不知道爲什麼。我發現了一個令人討厭的解決方法,但我想知道這裏發生了什麼。
有人可以對此有所瞭解嗎?
您能否介紹一下正確的(預期的)和錯誤的(實際的)路徑名的例子嗎?可能是Unicode或編碼問題? – 2010-08-10 06:26:59
我們認爲這也是第一位的,但路徑名不包含任何可能導致Unicode轉換問題的字符。 GetLongPathName的結果路徑名僅缺失最後4個字符(「Temp \」的「emp \」) - 所有其他字符都是有效的。 – Patrick 2010-08-10 08:07:28
聽起來像http://qc.embarcadero.com/wc/qcmain.aspx?d=92006 – 2016-08-29 15:57:21