[英]Overload File Function VB.NET
我正在嘗試通過ASP前端創建一個onclick函數,以檢查文件是否存在,如果不創建文件並向其寫入文本框文本,當前在以下代碼上顯示錯誤,提示我無法重載文件功能,是否存在更好的方法嗎?
更新問題是嘗試向其寫入文件時拋出錯誤,該文件仍處於打開狀態。
請參見以下代碼:
Protected Sub Create_Click(sender As Object, e As EventArgs)
Dim txtFile As String = "E:Documents\Visual Studio 2013\Projects\SomeProject\Templates\" & FileName.Text & ".txt"
If File.Exists(txtFile) Then
Response.Write("A file of that name already exists.")
Else
File.Create(txtFile)
File.WriteAllText(eTemplate.Text)
End If
End Sub
我也嘗試過:
If File.Exists(txtFile) Then
Response.Write("A file of that name already exists.")
Else
System.IO.File.Create(txtFile)
Dim sw As New StreamWriter(txtFile, True)
sw.Write(eTemplate.Text)
sw.Close()
End If
您的錢是對的,這是因為首先需要將其關閉。
我首先創建了一個文件流實例,創建了文件,將其關閉,然后寫入其中。 將以下代碼放入您的代碼中或將其寫出來,但請記住更正文件路徑。
Protected Sub Create_Click(sender As Object, e As EventArgs)
Dim txtFile As String = "E:\wherever\" & FileName.Text & ".txt"
If System.IO.File.Exists(txtFile) Then
Dim message As String = "A file by this name already exists, choose another or update the existing file."
Else
Dim fs As FileStream = File.Create(txtFile)
fs.Close()
Dim sw As New StreamWriter(txtFile, True)
sw.Write(eTemplate.Text)
sw.Close()
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.