Jump to content

Saving text to File where address is relative to what is saved

AliB2512

so I have a form with a multitude of textboxes (txtUsername, txtPassword, txtAddress.......)

and im after saving to a .txt file which is called based on what txtUsername is so, C:\Example\Example\Example\"txtUsername".txt

 

Public Class Form1
  Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
    If System.IO.File.Exists("\ ..\ ..\Resources\Users\<txtUsername>.txt")
    ) Then
        MsgBox("File Already Exists)")
    Else
        Dim objWriter As New System.IO.StreamWriter("\ ..\ ..\Resources\Users\<txtUsername>.txt" 
        objWriter.Write(txtsave.Text)
        objWriter.Close()
    End If
  End Sub
End Class

 

Link to comment
Share on other sites

Link to post
Share on other sites

Something along the lines of...

Dim Path As String
Path = "\ ..\ ..\Resources\Users\" + txtUsername.Text + ".txt"
If System.IO.File.Exists(Path)

...etc

Pretty self-explanatory. Creates a string variable called Path to compose the path in and then uses said variable in the IO functions.

Link to comment
Share on other sites

Link to post
Share on other sites

Hi 

If I can build on the above;

I would import the 'System.IO' namespace, just to make your code cleaner.

Then, I would put the ‘txtUsername.Text’ into a variable which will make it easier to work with i.e. is it missing? is it too long? or does the ‘txtUsername’ contain a character that cannot be used as a file name?

Do the same with the path of the file you want to write too.

Also, put anything that could fail into a Try / Catch format and catch any errors.

Thanks Andy

 

'Import the namespace 'System.IO' here
Imports System.IO

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles save.Click

        'This is variable to store the txtUsername.Text
        Dim FileNameToCheck As String = txtUsername.Text

        'This is variable to store the path
        Dim PathOfFile As String = "H:\" + FileNameToCheck + ".txt"

        If File.Exists(PathOfFile) Then
            MsgBox("File Already Exists")
            'Exit the sub if the file already exists
            Exit Sub
        End If


        'Put anything that could fail into a Try / Catch format
        Try
            Dim objWriter As New StreamWriter(PathOfFile)
            objWriter.Write(FileNameToCheck)
            objWriter.Close()
        Catch ex As Exception
            'Catch any errors here and feed it back in a MsgBox
            MsgBox(ex.ToString)
        End Try


    End Sub
End Class

 

Link to comment
Share on other sites

Link to post
Share on other sites

"\ ..\ ..\Resources\Users\<txtUsername>.txt"

This path string means you're starting from the root directory of the partition the program is currently running from. So it's going to try to use C:\Resources\Users\<txtUsername>.txt. Or rather, since there are spaces there: C:\ ..\ ..\Resources\Users\<txtUsername>.txt

 

If you want a relative path, you need to start with either one period (to signify the same directory) or two periods (to signify the previous directory)

Link to comment
Share on other sites

Link to post
Share on other sites

I wouldn't use what user enters as names for files.  Users have all kinds of weird names, very long names, names with lots of accents and other silliness, names with characters that aren't allowed to be used in file names and so on. See this which makes my point: http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/

 

You should simply have a default file in that folder called users.txt  or maybe users.xml since .net is so fond of using xml everywhere. You basically store there something like this

 

<users>
 <user id="1">John Smith</user>
 <user id="2">Smart Alec</user>
</users>

and then you simply create text files called user1.txt  and user2.txt and so on, where 1 and 2 match the ID in the xml file. Oh, and you could use xml to store the data in those files instead of using plain text as well.

 

Reading and writing xml files is super easy ... here's an example in C# : http://forum.codecall.net/topic/58239-c-tutorial-reading-and-writing-xml-files/

Here's example in visual basic .net : http://forum.codecall.net/topic/69309-writing-and-reading-xml-files-vbnet-part-i/

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×