Directories

crazymarvin

Member
Joined
Nov 22, 2006
Messages
18
Location
Belfast, UK
Programming Experience
Beginner
Hey, I want to save a file in a directory, but i needa do this multiple times.

I put this code under the form load event
VB.NET:
If Directory.Exists("Orders\") Then
            Exit Sub
        Else : Directory.CreateDirectory("Orders\" & DateString)
        End If

and im putting this for a button click event
VB.NET:
        filename = txtboxON.Text & " " & Mid(txtboxName.Text, 1, 1) & ".txt"
        Directory.SetCurrentDirectory("Orders\" & DateString)

        FileOpen(1, filename, OpenMode.Output)
        PrintLine(1, "Order Details")
        PrintLine(1)
        PrintLine(1, "Order Number:", txtboxON.Text)
        PrintLine(1)
        PrintLine(1, "Customer Name:", txtboxName.Text)
        PrintLine(1)
        PrintLine(1, "Address:", txtboxAdd.Text)
        PrintLine(1)
        PrintLine(1, "Post Code:", txtboxAdd2.Text)
        PrintLine(1)
        PrintLine(1, "Telephone Number", txtboxTel.Text)
        PrintLine(1)
        PrintLine(1, "Shape:")
        PrintLine(1, txtboxShape.Text)
        PrintLine(1)
        PrintLine(1, "Total Cost:", txtboxCost.Text)
        FileClose(1)

I need a way of stopping it from changing the directory every time, as at the moment what happens is it looks for, "\Orders\06-15-06\" the first time, and "\Orders\06-15-06\Orders\06-15-06\" the second time.

Can anyone help?

thanks, Martyn
 
Don't use SetCurrentDirectory and don't assume that the current directory is already pointing to your program folder. Also, don't use that old VB6-style of I/O.
VB.NET:
Dim folderPath As String = Path.Combine(Application.StartupPath, "Orders")

If Not Directory.Exists(folderPath) Then
    Directory.CreateDirectory(folderPath)
End If
VB.NET:
Dim folderPath As String = Path.Combine(Application.StartupPath, String.Format("Orders\{0:yy-dd-MM}", myDate))
Dim fileName As String = String.Format("{0} {1}.txt", txtboxON.Text, txtboxName.Text(0))

Using sw As New StreamWriter(Path.Combine(folderPath, fileName))
    sw.WriteLine("Order Details")
    'etc.
End Using
Also, I strongly recommend placing the month before the day in your folder names. That way alphabetical order matches chronological order.
 
Back
Top