Results 1 to 7 of 7

Thread: [RESOLVED] Creating and Writing to Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Resolved [RESOLVED] Creating and Writing to Text File

    I'm having trouble creating a text file, and writing a stored integer to it as a save text file.

    What I have is:
    (I've imported System.IO already)

    Dim id As Integer
    Dim writer As StreamWriter
    writer = File.CreateText("save.txt")

    writer.WriteLine(id)
    writer.Flush()
    writer.Close()


    When I run my program and select save, it doesn't create a save.txt file. What am I doing wrong?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Creating and Writing to Text File

    It certainly does create the file. It's just that you don't know where to look. Try specifying a directory and look there!

    writer = File.CreateText("C:\save.txt")
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Re: Creating and Writing to Text File

    I've tried specifying a directory, and that doesn't work either.
    If no directory is listed, it should save the file in the same directory that the app is run from (in this case my Debug folder) and it isn't there either.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Creating and Writing to Text File

    Then your code isn't executing at all ...

    Name:  a.png
Views: 586
Size:  22.8 KB
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Creating and Writing to Text File

    Not quite so Webberjo. If no path is specified, it will save the file in the current working directory. The default is the same as the folder as the .exe but it can be changed.
    I would suggest (if you haven't already done it) use Windows search to search for the filename you think you should have created to find out which directory it is actually being saved in.
    Also, you could use Application.StartupPath & "\save.txt" to ensure it is in the same directory as the .exe

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Re: Creating and Writing to Text File

    Stupid me, for some reason the handle on my save button was missing. No wonder nothing was happening.
    Thanks for the assistance!

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Creating and Writing to Text File

    Quote Originally Posted by Webberjo View Post
    Stupid me, for some reason the handle on my save button was missing. No wonder nothing was happening.
    Thanks for the assistance!
    Most likely because you deleted the control from the form then added it back from the toolbox and assumed the button event would still work like it would if you were using older classic VB.
    When you delete a control from a form VB removes all the Handle clauses from the created subs for the control, but the subs and the code in them remain.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width