覆盖另一个进程正在使用的文本文件? [英] Overwrite text file that is in use by another process?

查看:134
本文介绍了覆盖另一个进程正在使用的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个打开的日志文件,该文件正在被另一个程序使用,我用以下内容读取了该文件的内容:

    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)

谢谢.

解决方案

关于该主题还有很多其他文章.

检测文件是否被另一个进程(或实际上是同一进程)锁定了

如何检查文件锁定?

我可以简单地阅读'一个正在使用的文件?

解决方案似乎是:

Try
'Code to read file if its not locked by another app
Catch as System.IO.IOException

仅供参考,您的日志文件是非托管资源,因此确定性终结将有助于解决此资源争用问题.使用Using语句进行确定性的确定,例如:

Using fs = New FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
    fs.SetLength(0)
    file.Save(fs)
    fs.Flush()
End Using

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.

解决方案

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:

Try
'Code to read file if its not locked by another app
Catch as System.IO.IOException

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:

Using fs = New FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
    fs.SetLength(0)
    file.Save(fs)
    fs.Flush()
End Using

这篇关于覆盖另一个进程正在使用的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆