0

因此,在我的作业中,我们应该在Directories.txt文件中创建电话目录,然后更改目录中的列表。我制作了一个空白directories.txt文件并将其放在调试文件夹中。我还创建了一个按钮,标题为Create a New Phone Directory.当用户单击它时,会显示输入框,提示用户为新目录命名。我想知道如何从用户键入的输入框中获取结果,并使用它在directories.txt 中创建一个新目录文件并将其显示在列表框中。我想我必须使用流编写器,但每次尝试时,列表框中的结果都会显示为system.IO.streamwriter.

这是我当前的代码:

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

分配说明说使用写行方法将新文件的名称添加到directories.txt文件中,但我完全不知道如何做到这一点。任何帮助,将不胜感激!

4

1 回答 1

0
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

更新

更新以回答其他问题。在您的按钮单击事件中添加以下行

Listbox1.Items.Add(inputDirectory)

在内部 IF THEN 块之后添加行,这样您的代码就会像这样

    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)

请注意,您将需要几个文件来回答您的问题,因此您最终可能会得到类似

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
于 2013-04-15T01:42:24.060 回答