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