IAM从文件读取数据并将数据写入相同的IE IO发生异常,文件已被另一个进程使用 [英] Iam reading a data from file and writing data to same fie IO Exception is occured file is already used by another process

查看:102
本文介绍了IAM从文件读取数据并将数据写入相同的IE IO发生异常,文件已被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

lock(_syncobj)
{
    using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
    {
    // coding to read data
    }
    
    FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
    using (TextWriter tw = new StreamWriter(fileStream))
    {
    //coding to write data
    }
}


我已经使用FileShare.Read进行读取,并使用FileShare.None进行写入和使用锁定,但是还发生了一些IO异常,表明该文件已被另一个进程使用.


I have used FileShare.Read for reading and FileShare.None for writing and used lock also but also some IO Exception is occured stating that the file is used by another process.

推荐答案

做一件事

首先读取文件并将整个数据放在某个变量中,然后关闭打开的流
然后以写入模式打开它,然后将数据写入文件.
do one thing

first read the file and place the whole data in some variable then close the opened stream
and now open it in write mode and write the data to the file.


在仅读取文件的FileStream上,您需要将其设置为
On the FileStream that only READS the file, you need to set it as
FileShare.ReadWrite


这篇关于IAM从文件读取数据并将数据写入相同的IE IO发生异常,文件已被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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