阅读日志文件正被另一个进程 [英] Read log file being used by another process
问题描述
目标
我想按我的GUI中的按钮,在seclog.log文件从读(赛门铁克AV日志)远程计算机并显示日志的内容在我的应用程序丰富的文本框。
的事物,它们
一切,但读取日志文件
错误消息
System.IO.IOException是未处理的
消息=该进程无法访问该文件'\\HOSTNAME\C $ \Program文件(x86)\Symantec\Symantec端点Protection\seclog.log',因为它正在使用另一个进程。
来源= mscorlib程序
代码
//可能seclog路径
字符串seclogPath1 = @\\\\+目标+\\C $ \\Program文件(x86)\\Symantec\\Symantec端点Protection\\seclog.log
字符串seclogPath2 = @\\\\+目标+\\C $ \\Program Files\\Symantec\\Symantec端点Protection\\ seclog.log;
//如果要是(File.Exists(seclogPath1))
{
//output.AppendText(\"file存在于+ seclogPath1)seclog存在
;
// VAR seclogContent = File.Open(seclogPath1,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
流流= File.OpenRead(seclogPath1);
的StreamReader StreamReader的=新的StreamReader(流);
字符串str = streamReader.ReadToEnd();
output.AppendText(STR);
streamReader.Close();
stream.Close();
}
的资料我已经试过
谷歌搜索的问题。
以多种方式使用文件流
//可能seclog路径
字符串seclogPath1 = @\\\\+目标+\\ C $ \\Program文件(x86)\\Symantec\\Symantec端点Protection\\seclog.log
字符串seclogPath2 = @\\\\+目标+\\C $ \\Program Files\\Symantec\\Symantec端点Protection\\ seclog.log;
//如果要是(File.Exists(seclogPath1))
{
//output.AppendText(\"file存在于+ seclogPath1)seclog存在
;
// VAR seclogContent = File.Open(seclogPath1,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
流流= File.Open(seclogPath1,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
//File.OpenRead(seclogPath1);
的StreamReader StreamReader的=新的StreamReader(流);
字符串str = streamReader.ReadToEnd();
output.AppendText(STR);
streamReader.Close();
stream.Close();
}
我不得不改变什么
我要创建一个读写FILESTREAM
原代码
流流= File.OpenRead(seclogPath1);
的StreamReader StreamReader的=新的StreamReader(流);
新的代码
流流= File.Open(seclogPath1,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
//File.OpenRead(seclogPath1);
的StreamReader StreamReader的=新的StreamReader(流);
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.
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
Googling the issue
using filestreams in multiple ways
//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);
这篇关于阅读日志文件正被另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!