Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27348

VS 2010 Skipping Problem Lines

$
0
0
Hey Guys,

I have coded a small file formatter:

Code:

Module functions

    '// Keep our functions here...
    Function fixAccountFile(ByVal strAccount)

        '// First split the HOST/USERNAME/PASSWORD
        Dim stringArray As String() = strAccount.Split(" ")
        Dim stringURI As String
        Dim stringUser As String
        Dim stringPass As String
        Dim newURL As String = String.Empty
        Dim newStringURL As String = String.Empty

        stringURI = stringArray(0)
        stringUser = stringArray(1)
        stringPass = stringArray(2)

        If stringURI.StartsWith("www.") Then
            newStringURL = "http://" & stringURI
            'MessageBox.Show(newStringURL)
        Else
            newStringURL = stringURI
        End If

        '// The fix for the "?q=" sites...
        If (newStringURL.Contains("?q=")) Then

            '// Begin the clean up...
            Dim theStringUrl = newStringURL
            Dim theUri = New Uri(theStringUrl)
            Dim theDomain = theUri.GetLeftPart(UriPartial.Authority)

            '// Rebuilt URL...
            newURL = theDomain & "/?q=user&login&destination=node/add " & stringArray(1) & " " & stringArray(2)

        Else

            '// Begin the clean up...
            Dim theStringUrl = newStringURL
            Dim theUri = New Uri(theStringUrl)
            Dim theDomain = theUri.GetLeftPart(UriPartial.Authority)

            '// Rebuilt URL...
            newURL = theDomain & "/user/login?destination=node/add " & stringArray(1) & " " & stringArray(2)

        End If


        Return newURL

    End Function

End Module

and

Code:

Public Class formMain

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click

        Try

            '// Open the target file...
            OpenFileDialog1.Title = "Select accounts file..."
            OpenFileDialog1.InitialDirectory = "C:"
            OpenFileDialog1.ShowDialog()

            '// Display the file in the input area...
            txtBoxInput.Text = OpenFileDialog1.FileName.ToString()

            '// List ready to use...
            Dim list As New List(Of String)

            '// Open file with the Using statement.
            Using sReader As StreamReader = New StreamReader(OpenFileDialog1.FileName)

                '// Loop over each line in file, While list is Not Nothing.
                Do While sReader.Peek <> -1

                    '// Feed each account to the function form fixing...
                    Dim lineFixed As String = fixAccountFile(sReader.ReadLine)

                    'If (lineFixed.Contains("U:")) Then

                    '// Add to the saved listbox...
                    listBoxSaved.Items.Add(lineFixed)

                    'End If

                Loop

                '// Show the count...
                lblCount.Text = listBoxSaved.Items.Count & " Accounts have been formatted."
                lblCount.ForeColor = Color.Green

                '// Close up the streamreader
                sReader.Close()

            End Using

        Catch ex As Exception

            MessageBox.Show(ex.ToString)

        End Try


    End Sub

    Private Sub btnOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutput.Click

        '// Save the target file...
        SaveFileDialog1.Title = "Select accounts file..."
        SaveFileDialog1.InitialDirectory = "C:"
        SaveFileDialog1.ShowDialog()

        '// Display the file in the input area...
        txtBoxOutput.Text = SaveFileDialog1.FileName.ToString()

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        '// First make sure we have paths set...
        If (txtBoxInput.Text = "" Or txtBoxOutput.Text = "") Then

            MessageBox.Show("You need to set the input location and the save file location before formatting!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            '// Save the accounts...
            Using SW As New StreamWriter(txtBoxOutput.Text, True)

                For Each itm As String In listBoxSaved.Items
                    SW.WriteLine(itm)
                Next

                '// Prompt the user we have finished...
                MessageBox.Show("File successfully saved to " & txtBoxOutput.Text, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End Using

        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click

        '// Close program
        Me.Close()


    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '// Clear the listboxes
        listBoxSaved.Items.Clear()

        '// Reset the label...
        lblCount.Text = "0 Accounts Formatted Yet!"
        lblCount.ForeColor = Color.Red

    End Sub
End Class

Which works like it should, the problem is once it hits a problem line it pops up the error in a messagebox "MessageBox.Show(ex.ToString)" what i'm trying to do is once it hits a problem instead of popping up the error just to ignore and continue, i can't seem to find the best way to do it.

any help would be appreciated :)

cheers guys

Graham

Viewing all articles
Browse latest Browse all 27348

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>