4
我想创建一个文本文件并将一些文本写入此文件,但我的代码无法创建文本文件。创建一个文本文件并写入它
错误消息:
UnauthorizedAccessExcepion was unhandled by user code
Access to the path 'c:\save.txt' is denied.
我的代码:
Dim fileLoc As String = "c:\save.txt"
Dim fs As FileStream = Nothing
If (Not File.Exists(fileLoc)) Then
fs = File.Create(fileLoc)
Using fs
End Using
End If
If File.Exists(fileLoc) Then
Using sw As StreamWriter = New StreamWriter(fileLoc)
a = "Test: " + TextBox1.Text
c = "=============================================="
sw.Write(a)
sw.Write(c)
End Using
End If
它的工作! :]另外,如何在My Documents文件中创建txt文件? –
'SaveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);' – Isuru
Dim fileLoc As String = SaveFileDialog1.InitialDirectory +“\ save.txt”?? –