我正在使用BitConverter.GetBytes()将各种变量(不同类型)转换为字节数组,将其传递给自定义方法,我需要检查每个字节的值.
我注意到,我可以类型的变量传递byte到BitConverter.GetBytes()(即使它不是在过载列表中列出:参见相关MSDN页)在这种情况下,我总是有一个2字节数组返回值.我不应该将单字节数组作为返回值吗?.NET如何解释字节参数?
样品:
byte arg = 0x00;
byte[] byteArr = BitConverter.GetBytes(arg);
// Result: byteArr is a 2-bytes array where byte[0] = 0 and byte[ 1] = 0
Run Code Online (Sandbox Code Playgroud) 我最近编写了一个模拟用户帐户的应用程序,获取CURRENT_USER注册表项的句柄(使用PInvoke"LoadUserProfile"检索ProfileInfo.hProfile对象)并使用RegistryKey.FromHandle创建注册表项.
参考代码:
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(hToken))
{
using (SafeRegistryHandle safeHandle = new SafeRegistryHandle(hProfile, true))
{
using (RegistryKey impersonatedUserHkcu = RegistryKey.FromHandle(safeHandle, RegistryView.Default))
{
// Do something with registry
}
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码运行良好(在Windows 7中运行),但使用仅支持.NET 4.0及更高版本的对象/方法(SafeRegistryHandle,RegistryKey.FromHandle(),RegistryView枚举).
现在,我需要使这个应用程序与.NET 3.5兼容,以便在具有Windows XP的机器中使用它,并且无法安装.NET Framework 4.0.
我可以使用.NET 3.5来实现相同的结果吗?(即,对模拟用户的注册表项进行修改).或者确实存在只有-.NET4对象的某种源代码?