Results 1 to 3 of 3

Thread: Problem opening and saving text files.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    146

    Problem opening and saving text files.

    Okay so I have created a script that should open and save text files according to the date which has been selected on a datetimepicker control.

    In my Bin\Debug directory I have a text file named "30 December 2007.txt"

    and here is my code:

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Dim SR As StreamReader
    6.     Dim SW As StreamWriter
    7.  
    8.     Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    9.         Dim theDatePicked As String = DateTimePicker1.Text & ".txt"
    10.         SR = New StreamReader(theDatePicked)
    11.         TextBox1.Text = SR.ReadToEnd
    12.     End Sub
    13.  
    14.     Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    15.         Dim theDatePicked As String = DateTimePicker1.Text & ".txt"
    16.         SW = New StreamWriter(theDatePicked)
    17.         SW.Write(TextBox1.Text)
    18.         SW.Flush()
    19.     End Sub
    20.  
    21.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    22.         Dim theDatePicked As String = DateTimePicker1.Text & ".txt"
    23.         SR = New StreamReader(theDatePicked)
    24.         TextBox1.Text = SR.ReadToEnd
    25.     End Sub
    26. End Class

    I keep getting this error message when I try to debug it.

    An error occurred creating the form. See Exception.InnerException for details. The error is: Could not find file 'C:\Users\Matt's Computer\Documents\Visual Studio 2008\Projects\iDiary\iDiary\bin\Debug\.txt'.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem opening and saving text files.

    You have this:

    Dim theDatePicked As String = DateTimePicker1.Text & ".txt"

    Try this:

    Dim theDatePicked As String = DateTimePicker1.Value & ".txt"
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem opening and saving text files.

    I strongly suggest that if you want to name files by date that you use a sortable format, so that chronological order matches alphabetical order. I also strongly suggest that you close your files after using them. Assuming that you're using VB 2005 this is all very simple:
    vb.net Code:
    1. Me.TextBox1.Text = IO.File.ReadAllText(IO.Path.Combine(Application.StartupPath, _
    2.                                                        String.Format("{0:yyyyMMdd}.txt", _
    3.                                                                      Me.DateTimePicker1.Value)))
    vb.net Code:
    1. IO.File.WriteAllText(IO.Path.Combine(Application.StartupPath, _
    2.                                      String.Format("{0:yyyyMMdd}.txt", _
    3.                                                    Me.DateTimePicker1.Value)), _
    4.                      Me.TextBox1.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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