【问题标题】:Reading the target of a .lnk file in C# .NET Core?在 C# .NET Core 中读取 .lnk 文件的目标?
【发布时间】:2020-09-29 19:19:52
【问题描述】:

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

【问题讨论】:

    标签: c# .net-core


    【解决方案1】:

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

    https://stackoverflow.com/a/28952464/11530367

    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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 2018-07-01
      相关资源
      最近更新 更多