我该如何编写这段代码而不会出错

时间:2016-02-07 16:41:35

标签: visual-studio

我想编写将某些东西写入文件的代码,但它说它不能。我怎样才能解决这个问题?请帮忙。

Imports System.IO

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles  MyBase.Load
    yes.Visible = False
    no.Visible = False
    Label1.Visible = False
    ProgressBar1.Visible = False
    Label2.Visible = False
    Label3.Visible = False
    TextBox1.Visible = False
    TextBox2.Visible = False
    apply.Visible = False
    back.Visible = False
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Button1.Visible = False
    yes.Visible = True
    no.Visible = True
    Label1.Visible = True
    setings.Visible = False

End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles yes.Click
    Label1.Text = "dowloading"
    no.Visible = False
    yes.Visible = False

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles no.Click
    yes.Visible = False
    no.Visible = False
    Label1.Visible = False
    Button1.Visible = True
    setings.Visible = True
End Sub

Private Sub setings_Click(sender As Object, e As EventArgs) Handles setings.Click
    Label2.Visible = True
    Label3.Visible = True
    TextBox1.Visible = True
    TextBox2.Visible = True
    apply.Visible = True
    back.Visible = True
    Button1.Visible = False
    setings.Visible = False
End Sub

Private Sub back_Click(sender As Object, e As EventArgs) Handles back.Click
    Label2.Visible = False
    Label3.Visible = False
    TextBox1.Visible = False
    TextBox2.Visible = False
    apply.Visible = False
    back.Visible = False
    Button1.Visible = True
    setings.Visible = True
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub apply_Click(sender As Object, e As EventArgs) Handles apply.Click
    Dim forgepath = TextBox1.Text
    Dim savefolder = Path.Combine(TextBox2.Text, "crazydolphininstaller")
    Directory.CreateDirectory(savefolder)
    Dim configfolder = Path.Combine(savefolder, "config")
    Directory.CreateDirectory(configfolder)
    Dim configfile = Path.Combine(configfolder, "config.txt")
    File.Create(configfile)
    Using writer = New StreamWriter(configfile)
        writer.WriteLine(forgepath)
        writer.WriteLine(savefolder)

    End Using
End Sub

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged

End Sub

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

File.Create创建文件并返回为您打开的FileStream。因此,以下StreamWriter发现文件已打开且无法在其中写入。只需删除File.Create行并使用StreamWriter构造函数,该构造函数允许覆盖文件(如果存在)

Private Sub apply_Click(sender As Object, e As EventArgs) Handles apply.Click
    Dim forgepath = TextBox1.Text
    Dim savefolder = Path.Combine(TextBox2.Text, "crazydolphininstaller")
    Dim configfolder = Path.Combine(savefolder, "config")
    ' Called just one time. All the folder missing will be created
    Directory.CreateDirectory(configfolder)
    Dim configfile = Path.Combine(configfolder, "config.txt")
    ' Not needed
    ' File.Create(configfile)

    ' Pass False as second parameter to overwrite the file if it exists
    Using writer = New StreamWriter(configfile, False)
        writer.WriteLine(forgepath)
        writer.WriteLine(savefolder)
    End Using
End Sub