为什么从.net应用程序运行批处理文件时没有任何输出?
在我的.Net应用程序中,我正在动态创建一个.bat文件,以便在网络上的另一台机器正在访问这些文件时,使用OPENFILES/QUery查找机器上所有打开的文件。如果我在.net应用程序之外运行该批处理文件,则该批处理文件将返回正确的输出,但如果我在该批处理文件内部执行该批处理文件,则该批处理文件将不会得到任何输出,而是运行同一批处理文件。代码如下:为什么从.net应用程序运行批处理文件时没有任何输出?,.net,vb.net,windows,batch-file,cmd,.net,Vb.net,Windows,Batch File,Cmd,在我的.Net应用程序中,我正在动态创建一个.bat文件,以便在网络上的另一台机器正在访问这些文件时,使用OPENFILES/QUery查找机器上所有打开的文件。如果我在.net应用程序之外运行该批处理文件,则该批处理文件将返回正确的输出,但如果我在该批处理文件内部执行该批处理文件,则该批处理文件将不会得到任何输出,而是运行同一批处理文件。代码如下: Dim curdir As String = System.IO.Directory.GetCurrentDirectory.ToStri
Dim curdir As String = System.IO.Directory.GetCurrentDirectory.ToString
File.Create(curdir & "\temp.bat").Close()
Using writer As New System.IO.StreamWriter(curdir & "\temp.bat")
writer.WriteLine("@echo ON")
writer.WriteLine("OPENFILES /Query > " & curdir & "\temp.txt")
writer.WriteLine("exit")
End Using
File.Create(curdir & "\temp.txt").Close()
Using Process As New Process
'Process.StartInfo.UseShellExecute = True
Process.StartInfo.FileName = curdir & "\temp.bat"
Process.StartInfo.Verb = "runas"
Process.Start()
Process.WaitForExit()
Dim srOutput As New IO.StreamReader(curdir & "\temp.txt")
MessageBox.Show(srOutput.ReadToEnd)
End Using
我还尝试了以下方法:
Dim myprocess As New Process
'Program you want to launch execute etc.
myprocess.StartInfo.FileName = "c:\windows\SysWow64\openfiles.exe"
myprocess.StartInfo.Arguments = " /query"
'This is important. Since this is a windows service it will run
'even though no one is logged in.
'Therefore there is not desktop available so you better
'not show any windows dialogs
myprocess.StartInfo.UseShellExecute = False
myprocess.StartInfo.CreateNoWindow = True
'We want to redirect the output from the openfiles call to the program
'Since there won't be any window to display it in
myprocess.StartInfo.RedirectStandardOutput = True
Try
myprocess.Start()
If Not (myprocess.StandardOutput Is Nothing) Then
'This string is used to contain what openfiles program returns
Dim tmpstr2 As String = String.Empty
Dim values(6) As Object 'This storeds the fields from openfiles
Dim values2(0) As Object 'This is the current date
values2(0) = DateTime.Now
Dim cnt As Integer = 0
Do
tmpstr2 = myprocess.StandardOutput.ReadLine
' Add some text to the file.
If Not (tmpstr2 Is Nothing) Then
cnt += 1
'The output is fixed length
If cnt > 5 Then
values(0) = tmpstr2.Substring(0, 15).Trim 'Host name
values(1) = tmpstr2.Substring(16, 8).Trim 'ID
values(2) = tmpstr2.Substring(25, 20).Trim 'accessed by
values(3) = tmpstr2.Substring(46, 10).Trim 'type
values(4) = tmpstr2.Substring(57, 10).Trim 'locks
values(5) = tmpstr2.Substring(68, 15).Trim 'open mode
values(6) = tmpstr2.Substring(84) 'open file
End If
End If
File.Create(curdir & "\temp.ini").Close()
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Host Name", values(0), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "ID", values(1), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Accessed by", values(2), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Type", values(3), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Locks", values(4), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Open mode", values(5), curdir & "\temp.ini")
INIWrapper.WriteINIValue2("OpenFiles" & cnt.ToString, "Open File", values(6), curdir & "\temp.ini")
Loop Until tmpstr2 Is Nothing
End If
'Wait for the process to finish before continuing.
myprocess.WaitForExit()
Catch e As System.Exception
Finally
'Free resources
myprocess.Close()
End Try
无济于事
编辑:
我刚刚注意到命令行抛出了一个错误:
错误:目标系统必须运行32位操作系统
有人知道如何在OPENFILES.exe中解决这个问题吗?此命令在我的应用程序之外运行。我认为/希望在没有明确权限的情况下阻止此类操作,因为您可以使用.batI命令删除整个硬盘数据。我只需要能够运行此命令,或者找到另一种方法来获取OPENFILES/Query命令将返回的所有打开文件的列表。这只是我最初想到的方法,在我最初的研究中没有发现替代方法。你试过运行64位版本的C:\Windows\System32\openfiles.exe吗?我确实试过,但没有成功。看看-C++/C解决方案。或者看看,一个VB.net解决方案,它也使用openfiles.exe。