在 C# .NET Core 中读取 .lnk 文件的目标?

GLJ*_*GLJ 3 c# .net-core

查看 .lnk 文件目标的其他解决方案需要使用 .NET Framework。我想从 .NET Core 读取 .lnk 文件的目标,而不使用与 .NET Framework 的互操作(特别是Shell32.Shell方法)。如果有任何不需要第三方库的解决方案,如果可能的话,我更愿意使用它们。但是,我无法在 .NET Core 标准库中找到答案。

GLJ*_*GLJ 7

使用我发现的在 Python 中实现的解决方案,我用 C# 重写了该函数。

/sf/answers/2026672511/

public static string GetLnkTargetPath(string filepath)
{
    using (var br = new BinaryReader(System.IO.File.OpenRead(filepath)))
    {
        // skip the first 20 bytes (HeaderSize and LinkCLSID)
        br.ReadBytes(0x14);
        // read the LinkFlags structure (4 bytes)
        uint lflags = br.ReadUInt32();
        // if the HasLinkTargetIDList bit is set then skip the stored IDList 
        // structure and header
        if ((lflags & 0x01) == 1)
        {
            br.ReadBytes(0x34);
            var skip = br.ReadUInt16(); // this counts of how far we need to skip ahead
            br.ReadBytes(skip);
        }
        // get the number of bytes the path contains
        var length = br.ReadUInt32();
        // skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlgas, and VolumeIDOffset)
        br.ReadBytes(0x0C);
        // Find the location of the LocalBasePath position
        var lbpos = br.ReadUInt32();
        // Skip to the path position 
        // (subtract the length of the read (4 bytes), the length of the skip (12 bytes), and
        // the length of the lbpos read (4 bytes) from the lbpos)
        br.ReadBytes((int)lbpos - 0x14);
        var size = length - lbpos - 0x02;
        var bytePath = br.ReadBytes((int)size);
        var path = Encoding.UTF8.GetString(bytePath, 0, bytePath.Length);
        return path;
    }
}
Run Code Online (Sandbox Code Playgroud)