How to get folder path from Known folder GUID in Delphi

会有一股神秘感。 提交于 2019-12-22 05:13:15

问题


I have a KNOWNFOLDERID and I would like to know the corresponding path like C:....\folder.

KNOWNFOLDERID can be found here. http://msdn.microsoft.com/en-us/library/bb762584%28VS.85%29.aspx

I d like to use win api (I don't want to build an array with all KNOWNFOLDERID and paths).

Thanks


回答1:


Simply call the SHGetKnownFolderPath API function.

Since this function was added in Vista, it won't be declared in the library units that shipped with Delphi 7. So you'd need to declare it yourself.

type
  KNOWNFOLDERID = TGuid;

function SHGetKnownFolderPath(
  const rfid: KNOWNFOLDERID;
  dwFlags: DWORD; 
  hToken: THandle; 
  out ppszPath: PWideChar
): HResult; stdcall; external 'Shell32.dll';

Now, since this function was added in Vista, attempts to call it on XP will lead to failures. So, I would recommend dealing with this by using CSIDL functions rather than the Vista known folder APIs.




回答2:


You cannot build an array of known folder ids and paths as there is no assurance the paths will be the same in every system. There are default paths for known folders but they are just defaults, they can be changed. Many corporate environments do this to, for example, move the user's documents folder to a network share that can be backed up more easily.

In any case a link in the link you provided contains all the information you need:

SHGetKnownFolderPath is the Win API function that returns the path of the known folder. Note that you need to release the unicode char pointer returned yourself by calling CoTaskMemFree.

If you preffer working with a COM object, you can use IKNOWNFOLDER instead.



来源:https://stackoverflow.com/questions/14802473/how-to-get-folder-path-from-known-folder-guid-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!