?
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
Medium Priority
?
307 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 400 total points
ID: 36551397
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 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 36551643
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
ID: 36601870
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 86

Expert Comment

by:Mike Tomlinson
ID: 36601892
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 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