Frage

So, in my assignment, we're supposed to create phone directories in the Directories.txt file and then change the listings in the directories. I made a blank directories.txt file and placed that in the debug folder. I also have created a button titled Create a New Phone Directory. When a user clicks on it, the inputbox shows up prompting the user to title the new directory. I am wondering how to get the results from that inputbox that the user typed and use that to create a new directory file in directories.txt and display it in a listbox. I think I have to use stream writer but every time I try, the result in the listbox shows up as system.IO.streamwriter.

This is my current code:

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
    inputDirectories = InputBox("Please Enter the Name of the New Directory")
    If inputDirectories Is "" Then
        MessageBox.Show("Invalid Directory Name")
    End If
    Dim fileDirectories As IO.StreamWriter = IO.File.CreateText(inputDirectories)
    fileDirectories.WriteLine(inputDirectories)
End Sub

The assignment instructions say to use to write line method to add the name of the new file to the directories.txt file but I am totally lost on how to do this. Any help would be appreciated!

War es hilfreich?

Lösung

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
'
Dim path As String = "c:\temp\MyTestDirectory.txt"
Dim fileDirectories As System.IO.StreamWriter
Dim inputDirectory As String = ""
  '
  inputDirectory = InputBox("Please Enter the Name of the New Directory")
  If inputDirectory = "" Then
    MessageBox.Show("Invalid Directory Name")
  Else
    If System.IO.File.Exists(path) = False Then
      'The file does not exist so create a new file & add the inputted data
      fileDirectories = System.IO.File.CreateText(path)
      fileDirectories.WriteLine(inputDirectory)
      fileDirectories.Flush()
      fileDirectories.Close()
    Else
      'The file exists so append  file with the inputted data
      fileDirectories = System.IO.File.AppendText(path)
      fileDirectories.WriteLine(inputDirectory)
      fileDirectories.Flush()
      fileDirectories.Close()
    End If
  End If
  '
End Sub

Sub ReadDataBackNow()
  ' Open the file to read from one line at a time
  Dim path As String = "c:\temp\MyTestDirectory.txt"
  Dim DataStreamIn As System.IO.StreamReader = System.IO.File.OpenText(path)
  Dim TextLines As String = ""
  '
    Do While DataStreamIn.Peek() >= 0
      TextLines = TextLines & DataStreamIn.ReadLine()
    Loop
    DataStreamIn.Close()
    MsgBox(TextLines)
End Sub

UPDATE

Update to answer additional question. In your button click event add the following line

Listbox1.Items.Add(inputDirectory)

Add the line AFTER the inner IF THEN block so your code would like this

    If System.IO.File.Exists(path) = False Then
      'The file does not exist so create a new file & add the inputted data
      fileDirectories = System.IO.File.CreateText(path)
      fileDirectories.WriteLine(inputDirectory)
      fileDirectories.Flush()
      fileDirectories.Close()
    Else
      'The file exists so append  file with the inputted data
      fileDirectories = System.IO.File.AppendText(path)
      fileDirectories.WriteLine(inputDirectory)
      fileDirectories.Flush()
      fileDirectories.Close()
    End If
    Listbox1.Items.Add(inputDirectory)

Note that you will need several files to answer your question, so you may end up with something like

Directories.txt (contains list of directories)
   Friends_Directory.txt
   Workmates_Directory.txt
   Family_Directory.txt

 Friends_Directory.txt (contains list of friends)
   Bob 1234567890
   Angela 2345678901
   Steve 3456789012
   Ahmed 4567890123
   Fatima 5678901234

 Workmates_Directory.txt (contains list of workmates)
   CEO_Alan 0987654321
   Manager_Daisy 0876543219
   Foreman_Judy 0765432198
   Colleague_Jill 0654321987

 Family_Directory.txt
   Bro_Malcolm 1122334455
   Sis_Alisha 2233445566
   Moms 3344556677
   Pops 4455667788
   Uncle_Ben 5566778899
   Aunty_Sarah 6677889900
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top