Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Another approach than File.Delete(tcFile)
Message
From
30/05/2012 21:23:15
 
 
To
30/05/2012 18:00:56
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01544721
Message ID:
01544889
Views:
22
For information on this issue, here is the updated version of the ProcessFile class, including the Using\End Using syntax as well:
Namespace Framework

    Public Class ProcessFile

        Public cArguments As String = ""
        Public cDomain As String = ""
        Public cError As String = ""
        Public cFileName As String = ""
        Public cLog As String = ""
        Public cMessage As String = ""
        Public cOutput As String = ""
        Public cPassword As String = ""
        Public cUsername As String = ""
        Public cWorkingDirectory As String = ""
        Public lRedirectStandardError As Boolean = True
        Public lRedirectStandardInput As Boolean = True
        Public lRedirectStandardOutput As Boolean = True
        Private oApp As Framework.App = Nothing
        Private oProcess As Process = Nothing
        Private oLXProcess As Framework.LXProcess = Nothing

        ' This is when we access the class in a desktop mode
        Public Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
        End Sub

        ' This is when we access the class in a Web or Web Service mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oLXProcess = toProcess
            oApp = oLXProcess.oApp
        End Sub

        Public Function Process() As Boolean
            Dim loSecureString As System.Security.SecureString = New System.Security.SecureString()
            Dim lnCounter As Integer = 0

            ' Reset the values
            cError = ""
            cLog = ""
            cMessage = ""
            cOutput = ""

            ' Trim everything
            cArguments = Trim(cArguments)
            cDomain = Trim(cDomain)
            cPassword = Trim(cPassword)
            cUsername = Trim(cUsername)

            ' If we have a working directory
            If cWorkingDirectory.Length > 0 Then

                ' If the directory does not exist
                If Not oApp.DirectoryExist(cWorkingDirectory) Then
                    cMessage = "The directory " + cWorkingDirectory + " does not exist."
                    Return False
                End If

            End If

            ' If the file does not exist
            If Not oApp.FileExist(cFileName) Then
                cMessage = "The file " + cFileName + " does not exist."
                Return False
            End If

            ' Use the Using\End Using approach to make sure the memory is released no matter what. This covers an unexpected error
            ' before it reaches the end. It closes the resources automatically at the end.
            Using oProcess As New Process

                oProcess.StartInfo.FileName = cFileName
                oProcess.StartInfo.WorkingDirectory = cWorkingDirectory
                oProcess.StartInfo.Arguments = cArguments
                oProcess.StartInfo.RedirectStandardOutput = lRedirectStandardOutput
                oProcess.StartInfo.RedirectStandardInput = lRedirectStandardInput
                oProcess.StartInfo.RedirectStandardError = lRedirectStandardError
                oProcess.StartInfo.UseShellExecute = False
                oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
                oProcess.StartInfo.CreateNoWindow = True

                ' If we have a domain
                If cDomain.Length > 0 Then
                    oProcess.StartInfo.Domain = cDomain
                End If

                ' If we have a username
                If cUsername.Length > 0 Then
                    oProcess.StartInfo.LoadUserProfile = True
                    oProcess.StartInfo.UserName = cUsername

                    ' For each character
                    For lnCounter = 1 To cPassword.Length
                        loSecureString.AppendChar(cPassword(lnCounter - 1))
                    Next

                    oProcess.StartInfo.Password = loSecureString
                End If

                Try
                    oProcess.Start()
                Catch loError As Exception
                    cMessage = loError.Message + " " + cFileName + " " + cArguments
                    Return False
                End Try

                ' If we have redirected the error
                If lRedirectStandardError Then
                    cError = oProcess.StandardError.ReadToEnd()
                End If

                ' If we have redirected the output
                If lRedirectStandardOutput Then
                    cOutput = oProcess.StandardOutput.ReadToEnd()

                    ' If we have an output
                    If cOutput.Length > 0 Then

                        ' If we have at least three characters
                        If cOutput.Length > 2 Then

                            ' If the last characters is a carriage return
                            If Mid(cOutput, cOutput.Length - 1, 2) = oApp.cCR Then
                                cOutput = Mid(cOutput, 1, cOutput.Length - 2)
                            End If

                        End If

                        ' Some output contains only CHR(13), so we will do some parsing to standardize all this
                        cOutput = oApp.StrTran(cOutput, oApp.cCR, "LXFramework" + Chr(0) + "LXFramework")
                        cOutput = oApp.StrTran(cOutput, Chr(13), oApp.cCR)
                        cOutput = oApp.StrTran(cOutput, "LXFramework" + Chr(0) + "LXFramework", oApp.cCR)

                    End If

                End If

                ' If we have an output
                If cOutput.Length > 0 Then
                    cLog = cLog + "Output" + oApp.cCR + "------" + oApp.cCR + cOutput
                End If

                ' If we have an error
                If cError.Length > 0 Then
                    cLog = cLog + "Error" + oApp.cCR + "-----" + oApp.cCR + cError
                End If

                ' Wait for the process to complete before proceeding
                oProcess.WaitForExit()

            End Using

            ' Reset the values
            cDomain = ""
            cPassword = ""
            cUsername = ""

            Return True
        End Function

    End Class

End Namespace
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform