theDeSilva.com Andrew de Silva

Using cmd.exe with VB.NET – Process.Start

January 27th, 2010 · No Comments · .NET

Here’s a quick function to show the use of System.Diagnostic.Process which will allow you to start another application using VB.NET. The example below is used to call svnadmin to create a new repository ( Subversion ) by grabbing the repository name. The function also checks to see whether the directory exist on the file system and if it doesn’t it will start the command prompt svnadmin to create the folder. However if the folder does exist it will nag at you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
Sub createRepo(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim repoName As String = repoNameInput.Text
 
        Dim dirExist As Boolean = False
 
        If System.IO.Directory.Exists("C:\svn_repository\" & repoName) Then
            dirExist = True
            result.Text = "<font color=""red"">Repository Already Exist !</font>"
        End If
 
        If (repoName.Length <> 0 And dirExist = False) Then
            Dim myProcess As Process = Process.Start("cmd.exe", "/k svnadmin create C:\svn_repository\" & repoName)
 
            myProcess.WaitForExit(10000)
            ' if the process doesn't complete within
            ' 10 seconds, kill it
 
            If Not myProcess.HasExited Then
                myProcess.Kill()
            End If
 
            result.Text = "Respository Created: " _
                & myProcess.ExitTime & _
                Environment.NewLine & _
                "Exit Code (Troubleshooting Purpose): " & _
                myProcess.ExitCode
        End If
 
    End Sub

You will have to import the following library to have the Process.Start to work.

1
Imports System.Diagnostics
Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn

Tags: ····