The LnkFileInfo class is a class that parses LNK files and shows information about them, such as their target path, their icon, etc. Although LNK files are Windows-specific, this library is cross-platform, which means that it can also be used on non-Windows systems to parse LNK files that have been copied there from Windows.
This class requires C++17 or later to work, there are no dependencies to third-party libraries. You can then use this library by downloading lnkfileinfo.hpp and putting it in the same folder as your source code. This library is header only, so to use it, you just have to do #include "lnkfileinfo.hpp"
.
-
LnkFileInfo()
Constructs an empty LnkFileInfo object. This object will be invalid until a new object is assigned to it.
-
LnkFileInfo(const std::string &file)
Constructs a new LnkFileInfo object that gives information about the given LNK file.
file
can be an absolute or a relative path. -
The copy constructor is automatically generated by the compiler.
-
bool operator==(const LnkFileInfo &other) const
Returns
true
if this LnkFileInfo object refers to the same LNK file asother
, andfalse
otherwise. The behavior is undefined if both LnkFileInfo objects are either empty or refer to LNK files that do not exist. -
bool operator!=(const LnkFileInfo &other) const
Returns
false
if this LnkFileInfo object refers to the same LNK file asother
, andtrue
otherwise. The behavior is undefined if both LnkFileInfo objects are either empty or refer to LNK files that do not exist. -
The copy assignment operator is automatically generated by the compiler.
-
std::string absoluteFilePath() const
Returns the absolute path of the LNK file itself, including the file name.
-
std::string absoluteTargetPath() const
Returns the absolute path of the target file. If the LNK file does not exist or is invalid, returns an empty string. If the LNK file exists and is valid but points to a nonexistent file, returns the absolute path of that nonexistent file.
-
std::string commandLineArgs() const
Returns the command line arguments of the LNK file, if any, not including the target. For example, if the LNK file points to
cmd.exe /v /c python.exe
, this method will return/v /c python.exe
. -
std::string description() const
Returns the description of the LNK file. The description is the custom text that appears when hovering over the LNK file in Windows Explorer, and can be edited in Windows Explorer by going to Properties -> Comment. If the LNK file has no custom description, this method returns an empty string.
-
bool exists() const
Returns
true
if the LNK file exists, regardless of whether or not it is a valid LNK file, and returnsfalse
otherwise. See alsoisValid()
andtargetExists()
. -
std::string filePath() const
Returns the path of the LNK file itself as specified in the constructor, including the file name. Can be absolute or relative.
-
bool hasCustomIcon() const
Returns
true
if the LNK file has a custom icon (including if the icon was manually set to be the same as its target), andfalse
if it doesn't (meaning the icon shown in Windows Explorer is the same as the target's icon). See alsoiconPath()
andiconIndex()
. -
std::string iconPath() const
If the LNK file has a custom icon, returns the path to the file containing that icon. Returns an empty string if the LNK file has no custom icon. See also
hasCustomIcon()
andiconIndex()
. -
int iconIndex() const
If the LNK file has a custom icon, returns the index of that icon in the icon file. Returns zero if the LNK file has no custom icon. See also
hasCustomIcon()
andiconPath()
. -
bool isValid() const
Returns
true
if the LNK file exists and is a valid LNK file (regardless of whether or not the target exists), andfalse
otherwise. See alsoexists()
andtargetExists()
. -
void refresh()
Re-reads all the information about the LNK file from the file system.
-
std::string relativeTargetPath() const
Returns the the path of the target relative to the LNK file, as specified in the LNK file. This can be useful for example if the LNK file and the target are both on a removeable drive for which the drive letter has changed, or if a common parent folder to the target and the LNK file has been moved or renamed. If this information is not present in the LNK file, returns an empty string.
This method only reads the information present in the LNK file, so the information might not be up to date.
-
bool targetExists() const
Returns
true
if the target exists, andfalse
otherwise. See alsoexists()
andisValid()
. -
bool targetHasAttribute(Attribute attribute) const
Returns true if the target has the attribute
attribute
, and false otherwise. The attribute is of typeLnkFileInfo::Attribute
.This method only reads the information present in the LNK file, so the information might not be up to date.
-
bool targetIsOnNetwork() const
Returns
true
if the target is on a network drive, andfalse
otherwise. -
unsigned int targetSize() const
Returns the size of the target file in bytes. This method only reads the information present in the LNK file, so the information might not be up to date. For up to date information, you can use
std::filesystem::file_size
. -
int targetVolumeSerial() const
Returns the serial number of the volume that the target is on. Returns zero if target is on a network drive.
-
VolumeType targetVolumeType() const
Returns the type of volume the target is on as a
LnkFileInfo::VolumeType
. -
std::string targetVolumeName() const
Returns the name of the drive the target is on as shown in the This PC folder if that drive has a custom name, and an empty string otherwise. Note that on most Windows computers, while the hard drive is called "Local Disk" by default, this is not a custom name so an empty string will be returnd in that case.
-
std::string workingDirectory() const
Returns the working directory specified in the LNK file. This can be edited in Windows Explorer by going to Properties -> Start in.
This enum is used together with the targetHasAttribute
method to check if the target has a given attribute according to the LNK file, and contains the following values:
ReadOnly = 0x0001
Hidden = 0x0002
System = 0x0004
VolumeLabel = 0x0008
Directory = 0x0010
Archive = 0x0020
NtfsEfs = 0x0040
Normal = 0x0080
Temporary = 0x0100
Sparse = 0x0200
ReparsePointData = 0x0400
Compressed = 0x0800
Offline = 0x1000
For more information about what each of these attributes mean, see File attribute - Wikipedia.
This enum is used together with the targetVolumeType
method to indicate which type of volume the target is on, and contains the following values:
Unknown = 0
NoRootDirectory = 1
Removable = 2
HardDrive = 3
NetworkDrive = 4
CdRom = 5
RamDrive = 6
Here is some example code that parses the Word.lnk shortcut on your desktop (if you have one). Note that the exact results may vary from one computer to another. Don't forget to change myname
to your Windows user name.
#include <iostream>
#include "lnkfileinfo.hpp"
int main(){
//Don't forget to make sure this file exists on your computer
LnkFileInfo wordLnk("C:/Users/myname/Desktop/Word.lnk");
std::cout << "Target path: " << wordLnk.absoluteTargetPath() << std::endl; //C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
std::cout << "Target path: " << wordLnk.relativeTargetPath() << std::endl; //..\..\..\..\..\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
std::cout << "Description: " << wordLnk.description() << std::endl; //Create beautiful documents, easily work with others, and enjoy the read.
std::cout << "Icon path: " << wordLnk.iconPath() << std::endl; //C:\Program Files\Microsoft Office\Root\VFS\Windows\Installer\{90160000-000F-0000-1000-0000000FF1CE}\wordicon.exe
std::cout << "Icon index: " << wordLnk.iconIndex() << std::endl; //0, meaning the icon of the LNK file is the first icon in the the wordicon.exe file
std::cout << "Target size: " << wordLnk.targetSize() << std::endl; //1637680, which is the size the WINWORD.EXE file in bytes at the time the LNK file was created
return 0;
}