In my .Net application, I am dynamically creating a .bat file to find all open files on the machine using OPENFILES /QUery (if another machine on the network is accessing them). This batch file returns the proper output if I run it outside of my .net application, but if I execute it inside it, I get no output at all, running the same exact batch file. Code below:
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
I have also tried the following:
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
to no avail.
EDIT:
I have just noticed that the command line is throwing an error: ERROR: The target system must be running a 32 bit OS
does anyone know how to get around this for OPENFILES.exe? This command works file outside of my application.