gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:58:58 30 4
gpt4 key购买 nike

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

最佳答案

使用我发现的在 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;
}
}

关于c# - 在 C# .NET Core 中读取 .lnk 文件的目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64126236/

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