2016-02-07 92 views
-3

我想編寫寫入文件的代碼,但它說不能。我怎樣才能解決這個問題?請幫忙。我該如何編寫這段代碼而沒有錯誤

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 
+0

錯誤說的是什麼? – Webster

+3

'「但它說它不能」' - 肯定有一個*更具體*表示的問題比。 「它不能」不是一個真正的標準錯誤信息。 – David

+0

@David「它說」中的「它」並不清楚。 IDE?編譯器?運行時? –

回答

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 
+0

thx幫助 –