gpt4 book ai didi

c# - PowerShell - 将 FileTime 转换为 HexString

转载 作者:太空狗 更新时间:2023-10-29 20:12:25 25 4
gpt4 key购买 nike

在搜索互联网后,我设法创建了一个 C# 类来获取 FileTimeUTC 十六进制字符串。

public class HexHelper
{
public static string GetUTCFileTimeAsHexString()
{
string sHEX = "";

long ftLong = DateTime.Now.ToFileTimeUtc();
int ftHigh = (int)(ftLong >> 32);
int ftLow = (int)ftLong;
sHEX = ftHigh.ToString("X") + ":" + ftLow.ToString("X");

return sHEX;
}
}

对于 PowerShell,我尝试使用相同的代码:

$HH = @"
public class HexHelper
{
public static string GetUTCFileTimeAsHexString()
{
string sHEX = "";

long ftLong = DateTime.Now.ToFileTimeUtc();
int ftHigh = (int)(ftLong >> 32);
int ftLow = (int)ftLong;
sHEX = ftHigh.ToString("X") + ":" + ftLow.ToString("X");

return sHEX;
}
}
"@;

Add-Type -TypeDefinition $HH;
$HexString = [HexHelper]::GetUTCFileTimeAsHexString();
$HexString;

问题是我收到了一些错误消息:

The name 'DateTime' does not exist in the current context

+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.

+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

我不知道如何使此 C# 代码对 PowerShell 有效,并且想要一个有效的解决方案。我不知道为什么 PowerShell 无法识别我的 C# 代码段中的 DateTime 类。

最佳答案

我在这里的回答在技术上不是对你问题的回答,因为它没有解决你的技术问题(你已经自己解决了)。

但是,您可能有兴趣知道使用基本上是 Powershell 的单行代码可以获得所需的结果:

function GetUTCFileTimeAsHexString
{
return `
(Get-Date).ToFileTimeUtc() `
| % { "{0:X8}:{1:X8}" -f (($_ -shr 32) -band 0xFFFFFFFFL), ($_ -band 0xFFFFFFFFL) }
}

$HexString = GetUTCFileTimeAsHexString
$HexString;

请注意,这至少需要 Powershell 3,它引入了 -shr-band 运算符。

关于c# - PowerShell - 将 FileTime 转换为 HexString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53437755/

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