How to work with other applications in VisualBasic.NET

Asked 2 years ago, Updated 2 years ago, 99 views

Nice to meet you!

I'm studying VisualBasic and I'm struggling.
As stated in the title, VB.NET is trying to manipulate other applications (such as command prompts) that started from VB applications themselves (commands sent → results obtained).

AppActivate (Process ID)
    SendKeys.SendWait("dir\{Enter}")

I can operate it like this, but if possible, I don't want to display the window itself.

The above method will cause the window to become active.

At first, I thought that the VB app would refer to the contents of the clipboard by copying the command prompt screen, but I don't think it's going to work as I want for the same reason.

Can someone help me?

Thank you for your cooperation!

vb.net

2022-09-30 19:21

3 Answers

For example, like this

Imports System
Imports System.IO
Imports System.Diagnostics

Module Sample
    Sub Main()
        Dim myProcess As Process = new Process()

        MyProcess.StartInfo.CreateNoWindow=true' window not created
        myProcess.StartInfo.FileName="cmd.exe"
        myProcess.StartInfo.UseShellExecute=false
        myProcess.StartInfo.RedirectStandardInput=true
        myProcess.StartInfo.RedirectStandardOutput=true

        myProcess.Start()
        Dimsr As StreamReader=myProcess.StandardOutput
        Dim myStreamWriter As StreamWriter=myProcess.StandardInput

        Write myStreamWriter.WriteLine("dir")' command as standard input
        myStreamWriter.Close()
        Dim returnvalue as String=sr.ReadToEnd()'Standard output read as string

        myProcess.WaitForExit()
        myProcess.Close()
        US>'Write to file with behavior check
        Dim file As StreamWriter = new StreamWriter(".\\out.txt")' Write Results as Files
        file.WriteLine(returnvalue)
        file.Close()
    End Sub
End Module


2022-09-30 19:21

Do I have to operate a program that has already started (in this case, a command prompt)?
It's overwhelmingly easier to launch a command prompt yourself to do what you want.
http://dobon.net/vb/dotnet/process/processwindowstyle.html
http://dobon.net/vb/dotnet/process/standardoutput.html


2022-09-30 19:21

If you hide the console window and don't redirect the standard input/output, you won't be able to use SendKeys.

SendKeys is the API that you operate on the active window and cannot be used for inactive windows that hide the window.You will be using the SendMessage API to control it.

To hide a window, you should be able to retrieve the window handle using the method found in the form embedding command prompt (cmd.exe) and then hide it in the ShowWindow API.


2022-09-30 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.