Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
305 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 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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