I'm writing a C# class that runs a Process (REG) to export registry keys. REG requires that you specify a filename to export to but I would rather have the output of REG directed to the standard output so I can capture it directly in my C# code (using Process.StandardOutput). Is there a way in PowerShell to specify the standard output as the filename?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If you have to use the REG program (rather than use PowerShell to query/dump the registry - or even just do it in the C# program itself), Probably the best you are going to get is to allow it to dump out to a temporary file, then pipe the contents of the file back to standard out and capture it in your C# program that way:
$guid = [Guid]::NewGuid().ToString("N")
REG EXPORT HKCU\Software\Microsoft\Windows "$env:temp\$guid" | Out-Null
Get-Content "$env:temp\$guid"
Remove-Item "$env:temp\$guid"
In case you were not aware: Using PowerShell, you can navigate the registry as though it were part of the file system. Perhaps this is helpful in some other regard?
cd HKCU:\Software\Microsoft\Windows
dir
回答2:
Just use 'CONOUT$' as the file name (as pojnted out in comments, this only works on Windows XP):
PS C:\> reg export HKLM\SOFTWARE\FileZilla 'CONOUT$'
■W i n d o w s R e g i s t r y E d i t o r V e r s i o n 5 . 0 0
[ H K E Y _ L O C A L _ M A C H I N E \ S O F T W A R E \ F i l e Z i l l a ]
" I n s t a l l _ D i r " = " C : \ \ P r o g r a m F i l e s \ \ F i l e Z i l l a "
" R u n i n S e c u r e M o d e " = " 0 "
" U s e R e g i s t r y " = " 0 "
" L a n g u a g e " = " E n g l i s h "
There are some UNICODE encoding issues in the output shown here, but you should be able to handle that in the buffer when you parse it.