如何避免这个新建文件并写信息出错

please_call_me_J 2010-05-20 07:08:54
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim b As System.IO.StreamWriter
Dim bb As New System.IO.FileStream("c:\2.txt", IO.FileMode.Open)
b = New System.IO.StreamWriter(bb)
b.Write("sdfljslkfj22" + vbCrLf)
b.Write("sdfljslkfj")
b.Close()
Dim a As System.IO.StreamReader
a = New System.IO.StreamReader("c:\2.txt")
TextBox1.Text = a.ReadLine
a.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As System.IO.File
IO.File.Create("c:\2.txt")
End Sub

创建好了,执行click事件,提示2.txt被进程占用,无法访问。如何解决
...全文
68 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-05-20
  • 打赏
  • 举报
回复
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As System.IO.File
IO.File.Create("c:\2.txt")
End Sub
你这里创建了,没释放。
稍微修改一点
IO.File.Create("c:\2.txt").Close()

就可以了。
please_call_me_J 2010-05-20
  • 打赏
  • 举报
回复
就是那个。用文件流打开文件,我才学这个,不是很熟悉。谢谢各位
兔子-顾问 2010-05-20
  • 打赏
  • 举报
回复
用完后记得立刻关闭文件就好了。
xrongzhen 2010-05-20
  • 打赏
  • 举报
回复

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim b As System.IO.StreamWriter
'Dim bb As New System.IO.FileStream("c:\2.txt", IO.FileMode.Open) FileStream是打开文件 StreamWriter再打开肯定就出错了啊
b = New System.IO.StreamWriter("c:\2.txt")'这句改成这样
b.Write("sdfljslkfj22" + vbCrLf)
b.Write("sdfljslkfj")
b.Close()
Dim a As System.IO.StreamReader
a = New System.IO.StreamReader("c:\2.txt")
TextBox1.Text = a.ReadLine
a.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As System.IO.File
IO.File.Create("c:\2.txt")
End Sub

xrongzhen 2010-05-20
  • 打赏
  • 举报
回复

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim b As System.IO.StreamWriter
//Dim bb As New System.IO.FileStream("c:\2.txt", IO.FileMode.Open) FileStream是打开文件 StreamWriter再打开肯定就出错了啊
b = New System.IO.StreamWriter("c:\2.txt")//这句改成这样
b.Write("sdfljslkfj22" + vbCrLf)
b.Write("sdfljslkfj")
b.Close()
Dim a As System.IO.StreamReader
a = New System.IO.StreamReader("c:\2.txt")
TextBox1.Text = a.ReadLine
a.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim c As System.IO.File
IO.File.Create("c:\2.txt")
End Sub
Forrest23 2010-05-20
  • 打赏
  • 举报
回复

Dim bb As System.IO.FileStream
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bb = IO.File.Create("C:\Users\1\Desktop\2.txt")
End Sub

Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim b As New System.IO.StreamWriter(bb)
b.Write("sdfljslkfj22" + vbCrLf)
b.Write("sdfljslkfj")
b.Close()
Dim a As System.IO.StreamReader
a = New System.IO.StreamReader("C:\Users\1\Desktop\2.txt")
TextBox1.Text = a.ReadLine
a.Close()
End Sub
'这样就可以了
xingyuebuyu 2010-05-20
  • 打赏
  • 举报
回复
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If System.IO.File.Exists("c:\2.txt") = False Then
Dim fs As System.IO.FileStream = IO.File.Create("c:\2.txt")
fs.Close()
fs.Dispose()
End If
End Sub


建立文件时文件流就被打开了,需要关闭后面才可以进行操作.

16,557

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧