Hello, I have been fiddling around with this and no luck so far. So I am hoping someone can help me.

I am making a time clock application. When you add an employee it creates a new directory using their name along with a .txt file used to store their Time In, Time Out, and Daily Hrs worked. Everything is working smoothly here. The selected employees Time In Time Out and Daily Hrs and saved to their .txt file.

I have been also trying to save each new employees name to a .txt file so you can load it up after the program closes and not have to enter them again. My .txt file saves and loads up fine but when it comes to saving the selected employee's Time In, Time Out, and Daily Hrs. My program crashes when I click ClockIn or ClockOut and gives me the error Illegal characters in file path. Right now I have the selected name from the list being displayed in the txtName text box, so I can use it just like I typed it in there, No Luck.

Here's my code

Thanks in advance, if anymore information is needed just let me know.
~ ScarWars9

 Private Sub btnAddEmployee_Click(sender As System.Object, e As System.EventArgs) Handles btnAddEmployee.Click

        Dim obj As Object = listEmployees.Items

        If obj.contains(txtName.Text) Then
            MessageBox.Show(txtName.Text & " already exists in the Employee name list")
        Else

            If txtName.Text.Trim().Length = 0 Then
                MessageBox.Show("Please enter an Employees Name and click add!")
            Else

                'Adds Employee Names to list
                listEmployees.Items.Add(txtName.Text)

                'Saves list of employee names to employee_name.txt
                Dim objwriter As New System.IO.StreamWriter("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\employee_name.txt", append:=True)
                objwriter.Write(txtName.Text & Environment.NewLine)
                objwriter.Close()

                Dim filepath As String = ("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\" & txtName.Text & "\" & txtName.Text & ".txt")

                'Creates Employee File for new Employee Created.
                IO.Directory.CreateDirectory("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\" & txtName.Text)

                If System.IO.File.Exists(filepath) Then
                    MessageBox.Show("A file has already been created for " & txtName.Text)

                Else
                    System.IO.File.Create("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\" & txtName.Text & "\" & txtName.Text & ".txt")
                    MessageBox.Show("A new file has been created for " & txtName.Text)
                End If

            End If
        End If

    End Sub

     Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click

        'Clears all items in ListBox
        listEmployees.Items.Clear()

        'Loads saved employee's names to eployee_name.txt
        streamer = IO.File.OpenText("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\employee_name.txt")
        Dim sName() As String = streamer.ReadToEnd.Split(vbNewLine)
        listEmployees.Items.AddRange(sName)
        streamer.Close()
    End Sub

     Public Sub btnClkIn_Click(sender As System.Object, e As System.EventArgs) Handles btnClkIn.Click

        listEmployees.SelectedItem = txtName.Text

        dtpTimeIn.CustomFormat = "h:mm tt"

        Dim sClockIn As String
        Dim ts As TimeSpan
        sClockIn = ts.Hours & "hrs. " & ts.Minutes & " min."
        sClockIn = dtpTimeIn.Text


        lblTimeIn.Text = "Time In:" & Environment.NewLine & sClockIn

        dtpTimeIn.CustomFormat = "hh:mm tt M/dd/y"

        clockIn = dtpTimeIn.Text

        'This is where my illegal character in path error happens
        Dim objClockwriter As New System.IO.StreamWriter("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\" & txtName.Text & "\" & txtName.Text & ".txt", append:=True)
        objClockwriter.Write(listEmployees.Text & "-    Time In: " & clockIn)
        objClockwriter.Close()
    End Sub


    Private Sub btnClkOut_Click(sender As System.Object, e As System.EventArgs) Handles btnClkOut.Click

        txtName.Text = listEmployees.SelectedItem

        dtpTimeIn.CustomFormat = "h:mm tt"

        Dim sClockOut As String
        Dim tspan As TimeSpan
        sClockOut = tspan.Hours & "hrs. " & tspan.Minutes & " min."
        sClockOut = dtpTimeIn.Text

        lblTimeOut.Text = "Time Out:" & Environment.NewLine & sClockOut

        dtpTimeIn.CustomFormat = "hh:mm tt M/dd/y"

        clockOut = dtpTimeIn.Text

        Dim dailyHrs As String
        Dim ts As TimeSpan = clockOut - clockIn
        dailyHrs = ts.Hours & "hrs. " & ts.Minutes & " min."

        'This is where my illegal character in path error happens
        Dim objwriter As New System.IO.StreamWriter("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\" & txtName.Text & "\" & txtName.Text & ".txt", append:=True)
        objwriter.Write("   Time Out: " & clockOut & "   Daily Hours Worked: " & dailyHrs.ToString & Environment.NewLine)
        objwriter.Close()
    End Sub

Does the employee name have any punctuation in it? If so you might need to strip those out of the name and store it without any punctuation.

On a side note I would suggest you use a public variable to store the main directory path("C:\Users\Scarlett Hill\Desktop\Time Clock\Employee Directory\"). If you ever have to change that path you'll have a nightmare of a time finding every place you use it.

I have fixed my problem!
I revamped my application.

Thank you for the suggestion, I'm new to vb.net so any suggestions help.
Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.