Solved

Modify VB.NET code to create error log and log error messages that occurr when running shell(setup.exe)

Posted on 2011-09-16
4
292 Views
Last Modified: 2012-05-12
I would greatly appreciate some assistance modifying the code shown below so that my program will create an error log file at C:\Temp\ErrLog.txt and save any error messages that occur while trying to invoke the "shell(MyPathVariable.... setup.exe...." portion of the code, as shown on line # 6.

If someone could re-work/tweak the code below, and paste the revised code in their answer, so that an error log is created with respect to running the ‘Shell(MyPathVariable & "\setup.exe /s /f1" & MyPathVariable & "\setup.iss")’ command, I would greatly appreciate it.

 
Public Class Custom_Install
    Dim MyPathVariable As String
    Private Sub Custom_Install_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyPathVariable = My.Application.Info.DirectoryPath.ToString()

        Shell(MyPathVariable & "\setup.exe /s /f1" & MyPathVariable & "\setup.iss")


        'Application.Exit()
    End Sub
End Class

Open in new window

0
Comment
Question by:ACECORP
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
Comment Utility
You need to trap the error in a Try...Catch structure. Here is the basic code.

The variable ex that is catched contains the most basic information about the error.

If it is not enough, then we need to know the type of exception you get or expect to get (FileNotFoundException, InvalidArgumentException, ...) and catch that spécific class of exception instead of the generic Exception.

Public Class Custom_Install
	Dim MyPathVariable As String
	Private Sub Custom_Install_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		MyPathVariable = My.Application.Info.DirectoryPath.ToString()


		Try
			Shell(MyPathVariable & "\setup.exe /s /f1" & MyPathVariable & "\setup.iss")
		Catch ex As Exception
			Dim file As New System.IO.StreamWriter("C:\Temp\ErrLog.txt")
			file.WriteLine(ex.ToString)
			file.Close()
		End Try



		'Application.Exit()
	End Sub
End Class

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
Comment Utility
Does it output back to the console?  If yes, you can redirect the standard and error output streams and capture them like this:
Public Class Custom_Install

    Private MyPathVariable As String

    Private Sub Custom_Install_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyPathVariable = My.Application.Info.DirectoryPath.ToString()

        Dim P As New Process
        P.StartInfo.FileName = MyPathVariable & "\setup.exe"
        P.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(P.StartInfo.FileName)
        P.StartInfo.Arguments = "/s /f1" & MyPathVariable & "\setup.iss"
        P.StartInfo.UseShellExecute = False

        P.StartInfo.RedirectStandardOutput = True
        AddHandler P.OutputDataReceived, AddressOf P_OutputDataReceived

        P.StartInfo.RedirectStandardError = True
        AddHandler P.ErrorDataReceived, AddressOf P_ErrorDataReceived

        P.EnableRaisingEvents = True
        AddHandler P.Exited, AddressOf P_Exited
        P.Start() ' Asynchronous

        P.BeginOutputReadLine() ' Asynchronous
        P.BeginErrorReadLine() ' Asynchronous
    End Sub

    Private Sub P_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        ' ... do something with "e.Data" ...
        Debug.Print(e.Data)
    End Sub

    Private Sub P_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        ' ... do something with "e.Data" ...
        Debug.Print(e.Data)
    End Sub

    Private Sub P_Exited(ByVal sender As Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub

End Class

Open in new window

0
 
LVL 5

Author Comment

by:ACECORP
Comment Utility
So to actually write output and error data to two deparate text files, I am assuming I would need to do the following?

Or do I need to do something different to get that data to actually write to the files shown in code?

 
Private Sub P_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        ' ... do something with "e.Data" ...
        Debug.Print(e.Data)
        Dim P_OutputData As New System.IO.StreamWriter("\%SystemDrive%\Documents and Settings\All Users\Documents\MyOutputData.txt")
        P_OutputData.WriteLine(e.ToString)
        P_OutputData.Close()
    End Sub

    Private Sub P_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs)
        ' ... do something with "e.Data" ...
        Debug.Print(e.Data)
        Dim P_ErrorData As New System.IO.StreamWriter("\%SystemDrive%\Documents and Settings\All Users\Documents\MyErrorData.txt")
        P_ErrorData.WriteLine(e.ToString)
        P_ErrorData.Close()
    End Sub

Open in new window

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
That would work...though it's only going to keep the last line in the file since you are creating it each time.

You could use something like this if you want it to append each time:    *Delete the files before launching each time.

    My.Computer.FileSystem.WriteAllText("\%SystemDrive%\Documents and Settings\All Users\Documents\MyOutputData.txt", e.Data & vbCrLf, True)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now