开发者

Running a .bat in the background

开发者 https://www.devze.com 2023-04-08 16:27 出处:网络
So I have this in my coding: vb Code: file = My.Computer.FileSystem.OpenTextFileWriter(\"c:\\command.bat\", False)

So I have this in my coding:

vb Code:

file = My.Computer.FileSystem.OpenTextFileWriter("c:\command.bat", False)
        file.WriteLine("@echo off")
        file.WriteLine("cd " & TextBox2.Text)
        file.WriteLine("adb shell dumpsys meminfo " & TextBox1.Text & " > C:\Sample.txt")
        file.Close()
        Shell("C:\command.bat") 

what I want it to do is to run the batch file without it opening if that makes sense. Right now this runs on a loop for 10 minutes and on every 2 second tick it开发者_JAVA百科 opens and then closes the .bat. Which is really annoying to see a .bat open and close every two seconds. Is there anyway to get this process to run silently in the background so the user doesnt even know that it is running?


Shell("C:\command.bat", AppWinStyle.Hide)

That will run the batch file but the window is hidden.

or use Process.Start as suggested by David. with WindowStyle = ProcessWindowStyle.Hidden


Here is an example on how to use Process.Start with a hidden window

    Dim params As String = "C:\command.bat"
    Dim myProcess As New ProcessStartInfo
    myProcess.FileName = "cmd.exe"
    myProcess.Arguments = params
    myProcess.WindowStyle = ProcessWindowStyle.Hidden
    Process.Start(myProcess)

if you run into the issue of file not found errors with the path you can try to add the following Windows API call and run your file path through this function as well.

'This would be declared at the top of your Form Code/Class Code
Private Declare Auto Function GetShortPathName Lib "kernel32" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As StringBuilder, _
ByVal cchBuffer As Integer) As Integer

And here is the function to return back a ShortPath (win98 style path (ie. c:/progra~1/myfolder/myfile.bat)

Public Function GetShortPath(ByVal longPath As String) As String
    Dim requiredSize As Integer = GetShortPathName(longPath, Nothing, 0)
    Dim buffer As New StringBuilder(requiredSize)

    GetShortPathName(longPath, buffer, buffer.Capacity)
    Return buffer.ToString()
End Function

then simply call your path like this in your process.start function

    Dim params As String = GetShortPathName("C:\command.bat")
0

精彩评论

暂无评论...
验证码 换一张
取 消