In program development, we may often need to deal with the creation and deletion of shortcuts to facilitate operations. But how to create and delete shortcuts is what this article describes.
Some people may think that creation is simpler than deletion. In fact, it is not the case. The problems to be considered and handled are roughly the same. For example, permission problems are a very important issue. If the problems are not handled properly, failure may occur.
1. create shortcuts
There are two shortcut methods: desktop and Startup menu.
(1) Desktop shortcuts
There is no big difference between a desktop shortcut and a general file. The main difference is to determine the desktop location, and then create a shortcut file or *. lnk file.
The problem is how to obtain the desktop path:
How to obtain the desktop path: either through the registry or by calling the api Function
Through the registry:
Stored in the root of HKEY_CURRENT_USER. The directory is Software \ MicroSoft \ Windows \ CurrentVersion \ Explorer. The actual physical directory corresponding to the preceding folder is stored in the shell folder segment.
Api functions:
You can call the shell function to obtain the corresponding directory, but not all folders under the shell folder directory can be obtained.
Function GetSpecialFolderDir (folderid: Integer): string; var pidl: pItemIDList; buffer: array [0 .. 255] of char; begin // get the specified folder project Table SHGetSpecialFolderLocation (application. handle, folderid, pidl); SHGetPathFromIDList (pidl, buffer); // convert it to the file system path Result: = strpas (buffer); end;
Here: folderid can take down the following values: but note that some are virtual folders, not part of the file system. Therefore, SHGetPathFromIDList cannot be used to retrieve paths, but it is also listed here. '*' Is not a real file system and should be used as it.
CSIDL_BITBUCKET * Recycle Bin
CSIDL_CONTROLS * Control Panel
CSIDL_DESKTOP * Desktop
Csidl_topics topdirectory Desktop directory // For example, C: \ WINDOWS \ Desktop
CSIDL_DRIVES * my computer
CSIDL_FONTS Font/such as C: \ WINDOWS \ FONTS
CSIDL_NETHOOD neighbor directory on the network // For example, C: \ WINDOWS \ NetHood
CSIDL_NETWORK * network neighbors
CSIDL_PERSONAL My Documents // such as C: \ My Documents ents
CSIDL_PRINTERS * printer
CSIDL_PROGRAMS Program Group // For example, C: \ WINDOWS \ Start Menu \ Programs
CSIDL_RECENT Recent document // such as C: \ WINDOWS \ Recent
CSIDL_SENDTO sent to // such as C: \ WINDOWS \ SentTo
CSIDL_STARTMENU Start Menu // For example, C: \ WINDOWS \ Start Menu
CSIDL_STARTUP start // For example, C: \ WINDOWS \ Start
CSIDL_TEMPLATES template // For example, C: \ WINDOWS \ ShellNew
Create a desktop shortcut:
Uses ActiveX, ComObj, StdCtrls, ShlObj, FileCtrl; procedure destroy (Sender: TObject); var tmpObject: gradient; tmpSLink: IShellLink; tmpPFile: IPersistFile; PIDL: PItemIDList; StartupDirectory: array [0 .. MAX_PATH] of Char; StartupFilename: String; LinkFilename: WideString; begin // create a shortcut to the desktop StartupFilename: = Application. exeName; tmpObject: = CreateComObject (CLSID_ShellLink); // create a convenient shell extension tmpSLink: = tmpObject as IShellLink; // obtain the interface tmpPFile: = tmpObject as IPersistFile; // used to store *. the interface tmpSLink for the lnk file. setPath (pChar (StartupFilename); // you can specify the path tmpSLink. setWorkingDirectory (pChar (ExtractFilePath (StartupFilename); // set the working directory SHGetSpecialFolderLocation (0, CSIDL_DESKTOPDIRECTORY, PIDL); // obtain the Itemidlist partition (PIDL, StartupDirectory) of the desktop ); // obtain the desktop path // you can use the recently introduced tmpSLink to obtain the desktop shortcut. setDescription ('My program description); tmpSLink. setIconLocation (Pchar (StartupFilename), 0); LinkFilename: = StartupDirectory + '\ my program. lnk '; tmpPFile. save (pWChar (LinkFilename), FALSE); // Save *. lnk file end;
(2) create a menu shortcut
Unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Registry, ActiveX, ShlObj, StdCtrls, comobj; type TForm1 = class (TForm) button1: TButton; procedure Button1Click (Sender: TObject); private {Private declarations} public {Public declarations} end; var Form1: TForm1; implementation {$ R *. dfm} procedure success (SourceFileName: string); var MyObject: IUnknown; MySLink: IShellLink; MyPFile: IPersistFile; Directory, LinkName, SubDirectory: string; WFileName: WideString; begin MyObject: = CreateComObject (CLSID_ShellLink); MySLink: = MyObject as IShellLink; MyPFile: = MyObject as IPersistFile; MySLink. setPath (PChar (SourceFileName); with TRegistry. create (KEY_READ) do try RootKey: = HKEY_CURRENT_USER; if OpenKey ('Software \ MicroSoft \ Windows \ CurrentVersion \ Explorer \ Shell Folders ', False) then begin Directory: = ReadString ('start Menu '); CloseKey; end; finally Free; end; LinkName: = ChangeFileExt (SourceFileName ,'. lnk '); LinkName: = ExtractFileName (LinkName); if Directory <> ''then begin SubDirectory: = IncludeTrailingPathDelimiter (Directory + '\' + 'programs \ '+ 'Grand'); CreateDir (SubDirectory); // You must create a folder before creating the sub-file WFileName: = SubDirectory + LinkName; end; MyPFile. save (PWChar (WFileName), False); end; procedure TForm1.Button1Click (Sender: TObject); begin CreateSTARTMENUShortcut (Application. exeName); end.
When creating a shortcut, we may only notice how to implement this function or function. However, we often forgetPermission issuesIn this case, the creation will fail, so pay special attention to the need to escalate permissions to solve the problem.
Ii. Delete shortcuts
To delete a shortcut and delete a file, you can find the path of the file and use the deleteFile method. The reason is that it exists at the same time.Permission problemsIf the permission is insufficient, these operations cannot be implemented.