以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  [求助]怎样判断一个文本是否在占用?  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=146256)

--  作者:54292550
--  发布时间:2020/2/20 10:42:00
--  [求助]怎样判断一个文本是否在占用?
我在进行文本写入得时候怎么判断有其它进程在写入同一个文本?等不占用得时候我再操作。
--  作者:有点蓝
--  发布时间:2020/2/20 11:48:00
--  
试试

Dim fileName As String = "c:\\abc.txt"

Dim inUse As Boolean = True
Dim fs As system.io.FileStream = Nothing
Try
    fs = New system.io.FileStream(fileName, system.io.FileMode.Open, system.io.FileAccess.Read, system.io.FileShare.None)
    inUse = False
Catch ex As Exception
    
Finally
    If (fs IsNot Nothing) Then
        fs.Close()
    End If
End Try
msgbox(inUse) 

--  作者:54292550
--  发布时间:2020/2/20 13:08:00
--  
窗口计时器里面写了这个代码:

Dim fileName As String = "d:\\data.txt"
Dim inUse As Boolean = True
Dim fs As system.io.FileStream = Nothing
Try
    fs = New system.io.FileStream(fileName, system.io.FileMode.Open, system.io.FileAccess.Read, system.io.FileShare.None)
    inUse = False
Catch ex As Exception
   
Finally
    If (fs IsNot Nothing) Then
        fs.Close()
    End If
End Try
If inUse = True Then
e.Form.Controls("Label1").text = "空闲"
Else
e.Form.Controls("Label1").text = "占用"
End If




窗口上做了个按钮,写入文本得代码:
FileSys.WriteAllText("d:\\data.txt", "I Like Foxtable", True)




首次打开窗口得时候显示“空闲”,写入文本一次之后就一直是“占用”状态。怎么回事?

--  作者:有点蓝
--  发布时间:2020/2/20 13:44:00
--  
If inUse = True Then
e.Form.Controls("Label1").text = "占用"
Else
e.Form.Controls("Label1").text = "空闲"
End If