Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我可以在powershell中执行此操作吗?读取注册表,将二进制内容写入文件_.net_File_Powershell_Registry_Stream - Fatal编程技术网

.net 我可以在powershell中执行此操作吗?读取注册表,将二进制内容写入文件

.net 我可以在powershell中执行此操作吗?读取注册表,将二进制内容写入文件,.net,file,powershell,registry,stream,.net,File,Powershell,Registry,Stream,这是C代码。你能帮我把这个翻译成powershell吗 private static void Main(string[] args) { byte[] buffer = (byte[]) Registry.LocalMachine.OpenSubKey(@"HARDWARE\ACPI\DSDT\HP____\8510x\00010000").GetValue("00000000"); if (File.Exists("8510x.orig")) { Co

这是C代码。你能帮我把这个翻译成powershell吗

private static void Main(string[] args)
{
    byte[] buffer = (byte[]) Registry.LocalMachine.OpenSubKey(@"HARDWARE\ACPI\DSDT\HP____\8510x\00010000").GetValue("00000000");
    if (File.Exists("8510x.orig"))
    {
        Console.WriteLine("File 8510x.orig already exists.");
    }
    else
    {
        using (FileStream stream = new FileStream("8510x.orig", FileMode.CreateNew))
        {
            stream.Write(buffer, 0, buffer.Length);
            stream.Flush();
            stream.Close();
        }
        Console.WriteLine("Wrote 8510x.orig.");
    }
}
试试下面的方法

$a = gp "hklm:\HARDWARE\ACPI\DSDT\HP____\8510x\0001000" "00000000"
$a."00000000" | out-file 8610x.orig
我留下我以前的答案(上面)作为从PowerShell访问.NET对象的示例;但在看到基思·希尔的答案后,我不得不修改我的答案,以使用固定内容:

$a = gp HKLM:\HARDWARE\ACPI\DSDT\HP____\8510x\0001000 00000000

if ( test-path 8510x.orig )
{
   echo 'File 8510x.orig already exists.'
}
else
{
   $a.'00000000' | Set-Content 8510x.orig -enc byte 
   echo 'Wrote 8510x.orig'
}

您不想使用Out文件,因为它以字符串形式输出,并使用unicode引导。以下工作基于类似的注册表项:

$b = Get-ItemProperty HKLM:\HARDWARE\ACPI\DSDT\A7546\A7546011\00000011 00000000
$b.'00000000' | Set-Content foo.txt -enc byte

请注意,当您想要更直接地控制写入文件的内容时,设置内容非常有用,特别是当您想要写入原始字节时。

注意:out文件不会以二进制格式保存数据。它将获取字节数组,并将每个字节作为一个显示号写入。是的,输出文件不是一个好的选择。这意味着将数据写入文件的方式与控制台中显示的方式相同。
$b = Get-ItemProperty HKLM:\HARDWARE\ACPI\DSDT\A7546\A7546011\00000011 00000000
$b.'00000000' | Set-Content foo.txt -enc byte