TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Shell Function: Start EXE Program

Shell. Another application can be run from a VB.NET program. Using the Shell function, we specify a target executable and any command-line arguments needed. We learn ways to use the Shell function in practical situations.
Example. To begin, we specify a target application that happens to be stored in a specific location. If your computer has no procexp.exe file stored on the C volume, this will fail. You can change the file name to one that exists.

Next: When this program executes, the C:\procexp.exe program will be started and brought to focus on the computer.

VB.NET program that uses Shell function Module Module1 Sub Main() ' Run this specific executable on the shell. ' ... Specify that it is focused. Shell("C:\procexp.exe", AppWinStyle.NormalFocus) End Sub End Module
Example 2. How can we use Shell to run a command-line program that performs some other action? This is a practical way of using Shell. This example uses the 7-Zip executable program, which performs compression and decompression.

Here: This command line compresses all the text files on the C volume into a file called files.7z.

7-Zip Command-Line
VB.NET program that runs 7za.exe in Shell Module Module1 Sub Main() ' Run the 7-Zip console application to compress all txt files ' ... in the C:\ directory. Dim id As Integer = Shell("C:\7za.exe a -t7z C:\files.7z C:\*.txt") Console.WriteLine(id) End Sub End Module Output 1296 7-Zip (A) 9.07 beta Copyright (c) 1999-2009 Igor Pavlov 2009-08-29 [Output truncated]
Return value. The program also demonstrates the return value of the Shell function. This is the process ID of the program that was started. In this example, the 7za.exe program was started as process #1296.
Example 3. The Shell function does not work if you only pass the file name you want to open to it. Instead, you need to specify the application you want to open the file with. In this example, we use Notepad to open a target text file.

Also: You could use Microsoft Word or other applications located on the disk to open files.

VB.NET program that uses notepad to open file Module Module1 Sub Main() ' Use the notepad program to open this txt file. Shell("notepad C:\file.txt", AppWinStyle.NormalFocus) End Sub End Module
Internals. The Shell function in the VB.NET language and .NET Framework is implemented in managed code but uses lower-level constructs. It is not implemented with the Process type. The 2 types are separate.Process
Summary. The Shell function in the VB.NET language invokes external processes. The Process type provides a more object-oriented approach to this. But the Shell function is still useful. It is easier to recall and use.

Finally: It throws exceptions when files are not found, which can be handled in your program as well.

© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf