如何将数据保存到文本文件?

时间:2014-09-16 01:41:13

标签: vb.net

基本上,我有这个程序,你点击一个链接然后打开它。但它并没有保存我输入的所有链接。有一个名单和链接列表。您可以使用按钮添加链接和名称,然后打开“观看”链接。我主要是将它用于youtubers和streamer。这是代码。

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
        Dim nsr As StreamReader
        nsr = New StreamReader(NamePath)
        Do Until nsr.EndOfStream
            lstName.Items.Add(nsr.ReadLine)
        Loop
        nsr.Close()

        Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
        Dim usr As StreamReader
        usr = New StreamReader(URLPath)
        Do Until usr.EndOfStream
            lstURL.Items.Add(usr.ReadLine)
        Loop
        usr.Close()

    End Sub

    Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim NamePath As String = ("C:\Favoriter\NameList.txt\")
        Dim nsw As StreamWriter
        nsw = File.CreateText(NamePath)
        Dim NameItems As String
        Do Until lstName.Items.Count.Equals(0)
            NameItems = lstName.Items.Item(0)
            lstName.Items.RemoveAt(0)
            nsw.WriteLine(NameItems)
        Loop
        nsw.Flush()
        nsw.Close()

        Dim URLPath As String = ("C:\Favoriter\URLList.txt\")
        Dim usw As StreamWriter
        usw = File.CreateText(URLPath)
        Dim URLItems As String
        Do Until lstURL.Items.Count.Equals(0)
            URLItems = lstURL.Items.Item(0)
            lstURL.Items.RemoveAt(0)
            usw.WriteLine(URLItems)
        Loop
        usw.Flush()
        usw.Close()

    End Sub

    Private Sub lblTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTitle.Click

    End Sub

    Private Sub lstName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstName.SelectedIndexChanged

    End Sub

    Private Sub lstURL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstURL.SelectedIndexChanged

    End Sub

    Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged

    End Sub

    Private Sub txtURL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.TextChanged

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        lstName.Items.Add(txtName.Text)
        lstURL.Items.Add(txtURL.Text)

    End Sub

    Private Sub btnWatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click

        If lstURL.SelectedItem = True Then
            Process.Start(lstURL.SelectedItem)
        End If

    End Sub

    Private Sub lblName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblName.Click

    End Sub

    Private Sub lblURL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblURL.Click

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

首先我看到你正在使用File.CreateText(Path),这个函数会覆盖旧文件的数据,所以每次写完数据后你都会通过覆盖删除它们,你可以使用

if File.Exists(Path) then
File.Open(Path)
else
File.CreateText(Path)
End If
像这样,每次写文本时都不会覆盖。