Read & Write Command Prompt from VB .NET (Visual Basic 2010)
The Windows Command Prompt (cmd.exe) is an invaluable tool for developers— from automating routine tasks to troubleshooting complex issues. In this guide, we’ll explore:
- The use of Command Prompt for developers
- Common Command Prompt commands & their functions
- Integrating Command Prompt in Visual Studio
- Executing Command Prompt commands from VB.NET
- Example: Comparing two EXE files with the FC command
1. Why Developers Use the Command Prompt
While graphical user interfaces (GUIs) are user‑friendly, the Command Prompt offers:
- Automation—Batch files & scripts can run complex workflows unattended.
- Speed—Commands execute instantly without loading heavy UI elements.
- Control—Direct access to OS features like file systems, services, and network settings.
- Portability—Scripts written once on Windows often run on servers or in CI/CD pipelines.
- Lightweight troubleshooting—Diagnose and fix issues even when the GUI is unavailable.
2. Common Command Prompt Commands & Their Functions
Command | Description |
---|---|
dir |
List files and folders in the current directory. |
cd <folder> |
Change the working directory. |
copy <src> <dest> |
Copy files from source to destination. |
del <file> |
Delete one or more files. |
attrib |
View or change file attributes (read-only, hidden). |
ipconfig |
Network adapter configuration and IP address information. |
ping <host> |
Check network connectivity to a host. |
tasklist |
Display currently running processes. |
shutdown |
Shut down or restart the computer programmatically. |
fc |
Compare two files and show differences. |
3. Integrating Command Prompt in Visual Studio
You can open a Developer Command Prompt directly from Visual Studio:
- In Visual Studio, go to
Tools > Command Line > Developer Command Prompt
. - Use that prompt to run MSBuild, NuGet commands, or any scripts.
- Alternatively, add a
Pre-build
orPost-build
event in your project properties under the Build Events tab:
echo Running custom script... call "%ProgramFiles(x86)%\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools\csc.exe" /help
These events let you invoke command–line utilities as part of your build process, making automation seamless!
4. Executing Command Prompt Commands from VB.NET
In VB.NET, you can start a cmd.exe
process and capture its
input/output streams:
' VB.NET example: run a command and capture output
Imports System.Diagnostics
Public Sub RunCmd(command As String, args As String)
Dim psi As New ProcessStartInfo("cmd.exe")
psi.Arguments = $"/c {command} {args}"
psi.RedirectStandardOutput = True
psi.RedirectStandardError = True
psi.UseShellExecute = False
psi.CreateNoWindow = True
Dim proc As Process = Process.Start(psi)
Dim output As String = proc.StandardOutput.ReadToEnd()
Dim [error] As String = proc.StandardError.ReadToEnd()
proc.WaitForExit()
' Display results
Console.WriteLine(output)
If Not String.IsNullOrEmpty([error]) Then
Console.WriteLine("ERROR: " & [error])
End If
End Sub
Replace Console.WriteLine
with writing to a TextBox or
logging to file as needed in your Windows Forms or WPF application.
5. Example: Compare Two EXE Files Using fc
Let’s build a simple WinForms UI where the user enters two paths and clicks “Compare”:
- Add two TextBox controls (
txtPath1
,txtPath2
) and a Button (btnCompare
). - Add a multi-line TextBox (
txtResult
) to show differences.
Private Sub btnCompare_Click(sender As Object, e As EventArgs) Handles btnCompare.Click
Dim path1 As String = txtPath1.Text
Dim path2 As String = txtPath2.Text
RunCmd("fc", $"/b ""{path1}"" ""{path2}""")
' Assume RunCmd writes results to Console; capture into txtResult.Text instead
txtResult.Text = lastOutput
End Sub
When you run this, fc /b
does a byte‑by‑byte comparison
of the two EXE files and prints mismatched addresses, allowing you to
implement a “binary diff” directly in your VB.NET app.
♥ Here are some online Visual Basic lessons and courses: