因此,距离必须在VB.NET中进行编码已经有好几年了,我真的很生锈。我正在尝试将bcedit.exe的输出写入到计划以后读取的文件中。这是在x64系统上,我需要以admin身份运行命令。除了未写入文件外,其他都可以正常工作。
这是我到目前为止的内容:
Private Sub BCDEdit(strWhich As String)' This sub runs the Windows bcdedit program, with a paramater, edit the boot.ini file.
Dim p As System.Diagnostics.Process
Dim pStartInfo As System.Diagnostics.ProcessStartInfo
pStartInfo = New System.Diagnostics.ProcessStartInfo()
With pStartInfo
.FileName = Environment.SystemDirectory.ToString() & "\bcdedit.exe"
.Verb = "runas"
.Arguments = strWhich
.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
.UseShellExecute = True
End With
p = System.Diagnostics.Process.Start(pStartInfo)
p.WaitForExit()
End Sub
我用以下方式调用子:
BCDEdit("> " & Environment.GetEnvironmentVariable("TEMP") & "\DUMP.tmp".ToString())
似乎一半的挣扎与的值有关.UseShellExecute
,我需要将其设置True
为使命令提示符保持隐藏,但是,如果将其设置为,False
我可以稍微使用.RedirectStandardOutput
一下并将其读取到内存中(尽管我仍然没有也没有使输出的实际管道以这种方式工作)。
在bcdedit > C:\temp\dump.txt
提升的命令提示符下执行可以正常工作,因此翻译中会丢失一些内容。
任何人?
我怀疑重定向修饰符未被识别为参数。您可能必须通过重定向捕获它的标准输出流并将其输出到文件来手动重定向输出。
还似乎您将其倒退,UseShellExecute
应使用错误System.Diagnostics.ProcessWindowStyle.Hidden
。根据ProcessWindowStyle枚举
这是一个简单的例子:
Dim newprocess As New Process()
With newprocess.StartInfo
.CreateNoWindow = True
.FileName = Environment.SystemDirectory.ToString() & "\bcdedit.exe"
.Verb = "runas"
.RedirectStandardOutput = True
.UseShellExecute = False
End With
'Change the False to True to append instead of overwrite
Dim sw As New IO.StreamWriter(Environment.GetEnvironmentVariable("TEMP") & "\DUMP.tmp", False)
newprocess.Start()
Dim sr As IO.StreamReader = newprocess.StandardOutput
While Not sr.EndOfStream
sw.WriteLine(sr.ReadLine)
End While
sw.Close()
sr.Close()
不知道为什么'runas'不适用于该应用程序,但是我对此进行了更多思考,找到了一个可行的解决方案:
Dim newprocess As New Process()
With newprocess.StartInfo
.CreateNoWindow = True
.Verb = "runas"
.FileName = "cmd"
.Arguments = "/c bcdedit > " & Environment.GetEnvironmentVariable("TEMP") & "\DUMP.tmp"
.UseShellExecute = True
End With
newprocess.Start()
基本上,这是在管理员模式下使用命令处理程序来运行带有标准输出修饰符('>')的bcdedit。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句