FileMode和FileAccess和IOException:该进程无法访问文件“文件名",因为它正在被另一个进程使用 [英] FileMode and FileAccess and IOException: The process cannot access the file 'filename' because it is being used by another process

查看:83
本文介绍了FileMode和FileAccess和IOException:该进程无法访问文件“文件名",因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序A,它生成用于跟踪的文本文件.
同时,应用程序B需要读取相同的文本文件并附加在邮件消息中.

I have an application A that generates a text file for tracing.
While, an application B needs read the same text file and attach in a mailmessage.

但是当应用程序B尝试读取文本文件时,出现以下错误:

But I get the following error, when application B try read the text file:

IOException:该进程无法访问文件文件名",因为它 正在被另一个进程使用

IOException: The process cannot access the file 'filename' because it is being used by another process

有什么建议吗?也许更好地用于FileMode和FileAccess?

Any suggestions ? Maybe better use for FileMode and FileAccess?

应用程序A

if (File.Exists(nFile2)) File.Delete(nFile2);
                traceFile2 = File.Open(nFile2, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                if (traceFile2 != null)
                {
                    var twt2 = new TextWriterTraceListener(traceFile2);

                    // http://www.helixoft.com/blog/archives/20
                    try
                    {
                        if (twt2.Writer is StreamWriter)
                        {
                            (twt2.Writer as StreamWriter).AutoFlush = true;
                        }
                    }
                    catch { }

                    var indiceTraceFile2 = Trace.Listeners.Add(twt2);
                    System.Diagnostics.Trace.WriteLine("INICIO: " + DateTime.Now.ToString());

应用B

using (FileStream fileStream = File.Open(f, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                    {
                        var messageAttachment = new Attachment(fileStream, Path.GetFileName(f));
                        msgMail.Attachments.Add(messageAttachment);
                    }

推荐答案

您需要确保服务和阅读器都以非排他性方式打开日志文件.注意应用程序A的第2行和应用程序B的第1行

You need to make sure that both the service and the reader open the log file non-exclusively. Notice line 2 of App A and Line 1 of App B

应用程序A:

if (File.Exists(nFile2)) 
    File.Delete(nFile2);
traceFile2 =  new FileStream(nFile2, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
if (traceFile2 != null)
{
    var twt2 = new TextWriterTraceListener(traceFile2);
    // http://www.helixoft.com/blog/archives/20
    try
    {
        if (twt2.Writer is StreamWriter)
        {
            (twt2.Writer as StreamWriter).AutoFlush = true;
        }
    }
    catch { }

    var indiceTraceFile2 = Trace.Listeners.Add(twt2);
    System.Diagnostics.Trace.WriteLine("INICIO: " + DateTime.Now.ToString());

和应用B:

using (FileStream fileStream = new FileStream(f, FileMode.Open, 
                                                 FileAccess.Read, 
                                                 FileShare.ReadWrite))
{
    var messageAttachment = new Attachment(fileStream, Path.GetFileName(f));
    msgMail.Attachments.Add(messageAttachment);
}

这篇关于FileMode和FileAccess和IOException:该进程无法访问文件“文件名",因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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