gpt4 book ai didi

delphi - 如何保存 IShellLibary

转载 作者:行者123 更新时间:2023-12-03 15:54:48 25 4
gpt4 key购买 nike

以下代码将文件夹添加到 Libaries,但不保存它:

function SHAddFolderPathToLibrary(const plib: IShellLibrary;
pszFolderPath: LPCWSTR): HResult;
{ Corrected per discussion at StackOverflow }
var
psiFolder: IShellItem;
begin
Result := SHCreateItemFromParsingName(pszFolderPath, nil, IID_IShellItem,
psiFolder);
if Succeeded(Result) then
begin
Result := plib.AddFolder(psiFolder);
end;
end;

function AddFolderToLibrary(AFolder: string): HRESULT;
{ Add AFolder to Windows 7 library. }
var
plib: IShellLibrary;
begin
Result := CoCreateInstance(CLSID_ShellLibrary, nil, CLSCTX_INPROC_SERVER,
IID_IShellLibrary, plib);
if SUCCEEDED(Result) then
begin
Result := SHAddFolderPathToLibrary(plib, PWideChar(AFolder));
end;
end;

function RemoveFolderFromLibrary(AFolder: string): HRESULT;
{ Remove AFolder from Windows 7 library. }
var
plib: IShellLibrary;
begin
Result := CoCreateInstance(CLSID_ShellLibrary, nil, CLSCTX_INPROC_SERVER,
IID_IShellLibrary, plib);
if SUCCEEDED(Result) then
begin
Result := SHRemoveFolderPathFromLibrary(plib, PWideChar(AFolder));
end;
end;

最佳答案

您需要调用IShellLibrary.SaveInKnownFolder 。像这样:

uses
Winapi.KnownFolders;

var
hr: HRESULT;
plib: IShellLibrary;
si: IShellItem;
....
// your code to create plib is just fine
hr := plib.SaveInKnownFolder(FOLDERID_Libraries, 'My New Library',
LSF_MAKEUNIQUENAME, si);

为了完整起见,这里是一个最小的示例:

program W7LibraryDemo;

{$APPTYPE CONSOLE}

uses
System.SysUtils,
System.Win.ComObj,
Winapi.KnownFolders,
Winapi.ActiveX,
Winapi.ShlObj;

procedure Main;
var
sl: IShellLibrary;
si: IShellItem;
begin
OleCheck(CoCreateInstance(CLSID_ShellLibrary, nil, CLSCTX_INPROC_SERVER,
IID_IShellLibrary, sl));
OleCheck(sl.SaveInKnownFolder(FOLDERID_Libraries, 'My New Library',
LSF_MAKEUNIQUENAME, si));
end;

begin
try
CoInitialize(nil);
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

关于delphi - 如何保存 IShellLibary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17813924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com