C# 读取另一个进程正在使用的日志文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12942717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:02:27  来源:igfitidea点击:

Read log file being used by another process

c#fileremote-desktop

提问by toosweetnitemare

Goal

目标

I want to press a button on my GUI and read in the seclog.log file (symantec AV log) from a remote machine and display the contents of the log to a rich text box in my application.

我想在我的 GUI 上按下一个按钮并从远程机器读入 seclog.log 文件(symantec AV 日志)并将日志的内容显示到我的应用程序中的富文本框中。

Things That Work

有用的东西

everything but reading the log file

除了读取日志文件之外的所有内容

Error Message

错误信息

System.IO.IOException was unhandled
Message=The process cannot access the file '\HOSTNAME\C$\Program Files (x86)\Symantec\Symantec Endpoint Protection\seclog.log' because it is being used by another process.
Source=mscorlib

code

代码

//possible seclog paths
        String seclogPath1 = @"\\" + target + "\C$\Program Files (x86)\Symantec\Symantec Endpoint Protection\seclog.log";
        String seclogPath2 = @"\\" + target + "\C$\Program Files\Symantec\Symantec Endpoint Protection\seclog.log";

        //if seclog exists
        if (File.Exists(seclogPath1))
        {
            //output.AppendText("file exists at " + seclogPath1);
            //var seclogContent = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            Stream stream = File.OpenRead(seclogPath1);
            StreamReader streamReader = new StreamReader(stream);
            string str = streamReader.ReadToEnd();
            output.AppendText(str);
            streamReader.Close();
            stream.Close();


        }

Things I've Tried

我尝试过的事情

File is being used by another process

文件正被另一个进程使用

C# The process cannot access the file ''' because it is being used by another process

C#进程无法访问文件''',因为它正被另一个进程使用

Googling the issue

谷歌搜索问题

using filestreams in multiple ways

以多种方式使用文件流

采纳答案by toosweetnitemare

//possible seclog paths
String seclogPath1 = @"\\" + target + "\C$\Program Files (x86)\Symantec\Symantec Endpoint Protection\seclog.log";
String seclogPath2 = @"\\" + target + "\C$\Program Files\Symantec\Symantec Endpoint Protection\seclog.log";

//if seclog exists
if (File.Exists(seclogPath1))
{
    //output.AppendText("file exists at " + seclogPath1);
    //var seclogContent = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

    Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    //File.OpenRead(seclogPath1);
    StreamReader streamReader = new StreamReader(stream);
    string str = streamReader.ReadToEnd();
    output.AppendText(str);
    streamReader.Close();
    stream.Close();


}

what i had to change

我必须改变什么

i had to create a readwrite filestream

我必须创建一个读写文件流

original code

原始代码

Stream stream = File.OpenRead(seclogPath1);
StreamReader streamReader = new StreamReader(stream);

new code

新代码

Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//File.OpenRead(seclogPath1);
StreamReader streamReader = new StreamReader(stream);

回答by Kurkula

using (StreamReader sr = new StreamReader(filePath, true))
{
   sr.Close(); //This is mandatory
   //Do your file operation
}