覆盖另一个进程正在使用的文本文件?
我有一个日志文件已打开并被另一个程序使用,我通过以下内容读取了它的内容:
Dim strContents As String
Dim x As New FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim objReader As StreamReader
objReader = New StreamReader(x)
strContents = objReader.ReadToEnd()
objReader.Close()
这适用于在另一个程序仍在使用该文件时读取该文本,但是,在此之后我立即需要截断文本文件(不删除它),使其再次为空。但该文件仍将被其他程序使用。
我尝试过
Dim sWrite As StreamWriter
sWrite = New System.IO.StreamWriter(FullPath, False)
sWrite.Write("")
sWrite.Close()
,但出现“正在被另一个应用程序使用”异常。我尝试在 StackOverflow 和谷歌搜索中查找,但似乎找不到答案,而且我也找不到使用文件流执行此操作的方法,或者我会尝试
Dim fs As New FileStream(FullPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
提前使用“谢谢”。
I have a log file that is open and in use by another program, and I read it's contents with the following:
Dim strContents As String
Dim x As New FileStream(FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim objReader As StreamReader
objReader = New StreamReader(x)
strContents = objReader.ReadToEnd()
objReader.Close()
This works for reading the text from the file while it is still in use by the other program, however, immediately after this I need to truncate the text file (without deleting it) so that it is blank again. But the file will still be in use by the other program.
I tried
Dim sWrite As StreamWriter
sWrite = New System.IO.StreamWriter(FullPath, False)
sWrite.Write("")
sWrite.Close()
But I get the "in use by another application" exception. I've tried looking in StackOverflow and googling but I can't seem to find an answer, and I can't find a way to do this with filestreams either, or I would try to use
Dim fs As New FileStream(FullPath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于这个主题还有很多其他帖子。
检测文件是否被另一个进程(或者实际上是同一进程)锁定
如何检查文件锁定?
我可以简单地“读取”正在使用的文件吗?
解决方案似乎是:
另外仅供参考,您的日志文件是非托管资源,因此确定性最终确定将有助于解决此资源争用问题。使用Using语句进行确定性终结,例如:
There are a fair few other posts on this topic.
Detecting whether a file is locked by another process (or indeed the same process)
How to check for file lock?
Can I simply 'read' a file that is in use?
The solution appears to be:
Also just an FYI that your log file is unmanaged resource so deterministic finalization will help with this resource contention issue. Use the Using statement for deterministic finalization, eg:
那是因为其他程序锁定了该文件,操作系统不会让您做您想做的事情。除非您将其他程序更改为写入时不锁定。
Thats because the other program has a lock on the file the operating system won't let you do what you want to do. Unless you change the other program to no lock on write.
不幸的是,无论使用哪种语言,问题的解决方案都不是添加或修改几行代码那么简单。您正在描述资源争用的经典示例。
此类问题最好使用访问管理器来解决,即仲裁写入的另一个进程,以便您的程序不会覆盖其他程序所做的操作,反之亦然。
Unfortunately, the solution to the problem is not as simple adding or modifying a few lines code, regardless of the language being used. You are describing a classic example of resource contention.
This type of problem is best solved using an access manager, i.e. another process that arbitrates writes such that your program doesn't overwrite what the other program did and vice versa.
我不确定在其他进程关闭流之前您是否可以这样做。但是,您可以终止正在写入文件的进程。
这是一个执行此操作的示例。
看看这个:http://vbnetsample.blogspot.com/2007 /08/start-and-kill-process.html
I'm not sure if you can do that until the other process closes the stream. However, you can kill the process that is writing to the file.
Here's an example to do that.
Check this out: http://vbnetsample.blogspot.com/2007/08/start-and-kill-process.html