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
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
296 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
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 85

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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Assigning handler to UserControls in flowlayoutpanel 4 27
VB.Net Report Printing Issue 3 54
VB: Convert 2 dates to specific format 24 52
Write to a printer using vb.net 9 39
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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