24

ゴール

GUI のボタンを押して、リモート マシンから seclog.log ファイル (シマンテック AV ログ) を読み取り、ログの内容をアプリケーションのリッチ テキスト ボックスに表示したいと考えています。

機能するもの

ログファイルの読み取り以外のすべて

エラーメッセージ

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

コード

//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();


        }

私が試したこと

ファイルは別のプロセスによって使用されています

C# 別のプロセスで使用されているため、プロセスはファイル ''' にアクセスできません

問題をグーグルで調べる

複数の方法でファイルストリームを使用する

4

2 に答える 2

41
//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();


}

私が変えなければならなかったこと

読み書きファイルストリームを作成する必要がありました

オリジナルコード

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

新しいコード

Stream stream = File.Open(seclogPath1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//File.OpenRead(seclogPath1);
StreamReader streamReader = new StreamReader(stream);
于 2012-10-17T20:24:12.490 に答える
1
using (StreamReader sr = new StreamReader(filePath, true))
{
   sr.Close(); //This is mandatory
   //Do your file operation
}
于 2016-11-17T00:29:16.637 に答える