filestream锁住文件无法读取?

kukumaiwu 2010-04-19 09:52:46
用filestream写的一个生成文件的方法
该方法被循环调用
偶尔会出现文件正在使用中的错误提示(很少出现)
跟踪后发现停在new filestream这一句
用gc.collect都不行
请问是什么原因?
Public Function outputFile(ByVal strFilePath As String, ByVal str As String) As Boolean
Dim strPath As String = strFilePath
Dim strmFile As Stream
Dim ecdgSJIS As System.Text.Encoding = System.Text.Encoding.GetEncoding("shift_jis")
Dim strmwriWriter As StreamWriter
Dim intByteCnt As Integer = 0
Dim strMsg As String
Dim strFile As String = ""
Dim intCnt As Integer
Try
strmFile = New FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite)

If (File.GetAttributes(strPath) And FileAttributes.ReadOnly) = FileAttributes.ReadOnly Then
strMsg = MessageManager.GetString("E000028", CommonSyori.MsgPath, strPath, "", "", "")
MessageBox.Show(strMsg, CommonSyori.MsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand)
strmFile.Close()
Return False
Else
Try
strmFile = New FileStream(strPath, FileMode.Truncate, FileAccess.ReadWrite)
Catch ex As Exception
strMsg = MessageManager.GetString("E000027", CommonSyori.MsgPath, strPath, "", "", "")
MessageBox.Show(strMsg, CommonSyori.MsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand)
strmFile.Close()
strmFile.Dispose()
GC.Collect()
Return False
End Try
End If
If Me.setRenFileData(str) = False Then
strmFile.Close()
strmFile.Dispose()
GC.Collect()
Throw New Exception()
End If
strmwriWriter = New StreamWriter(strmFile, ecdgSJIS)
For intCnt = 0 To arrlKjFile.Count - 1
strmwriWriter.WriteLine(arrlKjFile(intCnt))
Next
strmwriWriter.Flush()
strmwriWriter.Close()
strmFile.Close()
strmFile.Dispose()
GC.Collect()
Catch ex As Exception
Return False
End Try
Return True
End Function
...全文
106 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
kukumaiwu 2010-04-19
  • 打赏
  • 举报
回复
读取位置代码
If Not outputFile(strOptRenPath, strOptNm) Then

strMsg = MessageManager.GetString("E000014", CommonSyori.MsgPath, "****文件生成", "", "", "")
MessageBox.Show(strMsg, CommonSyori.MsgTitle, MessageBoxButtons.OK, MessageBoxIcon.Hand)
End If

16,722

社区成员

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

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