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
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 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 400 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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 antispam), the admini…

734 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