|
Imports System.Text
Imports System.ComponentModel
Public Class ProcessData
Private _path, _args, _verb As String
Private WithEvents _worker As BackgroundWorker
#Region " Properties "
Public Property Path() As String
Get
Return Me._path
End Get
Set(ByVal value As String)
Me._path = value
End Set
End Property
Public Property Arguments() As String
Get
Return Me._args
End Get
Set(ByVal value As String)
Me._args = value
End Set
End Property
Public Property Verb() As String
Get
Return Me._verb
End Get
Set(ByVal value As String)
Me._verb = value
End Set
End Property
#End Region
#Region " Methods "
Public Shared Sub Start(ByVal path As String)
ProcessData.Start(path, String.Empty, String.Empty)
End Sub
Public Shared Sub Start(ByVal path As String, ByVal args As String)
ProcessData.Start(path, args, String.Empty)
End Sub
Public Shared Sub Start(ByVal path As String, ByVal args As String, ByVal verb As String)
Dim npd As New ProcessData
npd._path = path
npd._args = args
npd._verb = verb
npd._worker = New BackgroundWorker
npd._worker.RunWorkerAsync(npd)
End Sub
#End Region
#Region " Events "
Private Sub _worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles _worker.DoWork
If e.Argument.GetType Is GetType(ProcessData) Then
Dim executeInfo As ProcessData = DirectCast(e.Argument, ProcessData)
If executeInfo.Path <> String.Empty Then
Dim myProcess As New Process()
Dim startDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.System)
Try
myProcess.StartInfo.FileName = executeInfo._path
myProcess.StartInfo.Arguments = executeInfo._args
myProcess.StartInfo.Verb = executeInfo._verb
myProcess.StartInfo.WorkingDirectory = startDirectory
myProcess.Start()
myProcess.Close()
Catch ex As Exception
Dim sb As New StringBuilder()
sb.AppendLine(ex.Message)
If executeInfo._path <> String.Empty Then
sb.AppendLine()
sb.AppendLine("File:")
sb.AppendLine(executeInfo._path)
End If
If executeInfo._args <> String.Empty Then
sb.AppendLine()
sb.AppendLine("Arguments:")
sb.AppendLine(executeInfo._args)
End If
If executeInfo._verb <> String.Empty Then
sb.AppendLine()
sb.AppendLine("Verb:")
sb.AppendLine(executeInfo._verb)
End If
ErrorLog.ExceptionToFile(ex, TraceEventType.Error)
ErrorLog.EntryToFile(sb.ToString, TraceEventType.Information)
MessageBox.Show("Could not start the process for the folwoing reason(s):" _
& Environment.NewLine & sb.ToString, My.Application.Info.Title, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End If
End Sub
Private Sub _worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _worker.RunWorkerCompleted
Me._worker.Dispose()
Me._worker = Nothing
Me._args = Nothing
Me._path = Nothing
Me._verb = Nothing
End Sub
#End Region
End Class
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.