如何在string[]不获取IO异常的情况下读取也在Excel中打开的文本文件的所有行?
有一个问题可能是答案的一部分,虽然我不知道如何使用其中的内容: 如何使用.net StreamReader打开已打开的文件?
目标
我想按下GUI上的按钮并从远程计算机读取seclog.log文件(symantec 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
Run Code Online (Sandbox Code Playgroud)
码
//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 …Run Code Online (Sandbox Code Playgroud) 我不知道如何解决我的问题.我不时会收到错误:"进程无法访问文件'xxxx',因为它正被另一个进程使用".
这是我发生错误的方法:
private static void write_history(int index, int time_in_sec, int[] sent_resources)
{
string filepath = "Config\\xxx.txt";
int writing_index = 0;
if (File.Exists(filepath))
{
System.Threading.Thread.Sleep(5000);
StreamReader reader = new StreamReader(new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read));
string temp = reader.ReadToEnd();
reader.Close();
for (int i = 0; i < 20; i++)
{
if (temp.IndexOf("<hst_" + i.ToString() + ">") == -1)
{
writing_index = i;
break;
}
}
}
System.Threading.Thread.Sleep(5000);
// write to the file
StreamWriter writer = new StreamWriter(filepath, true);
writer.WriteLine("<hst_" + writing_index.ToString() + …Run Code Online (Sandbox Code Playgroud) 什么是更好的,使用指令,或完成对象时的dispose指令?
using(FileStream fileStream = new FileStream(
"logs/myapp.log",
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
{
using(StreamReader streamReader = new StreamReader(fileStream))
{
this.textBoxLogs.Text = streamReader.ReadToEnd();
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面,当我正在处理System.Net.Mail时,我被告知我需要对象的Dispose()来释放任何杂散锁.
有没有一致的指导?如何判断在给定对象的特定情况下哪些更合适?
我正在尝试读取某个程序正在使用的所有日志文件行。
当我尝试这样做时,我收到异常:
System.IO.IOException was unhandled : file used by another process
Run Code Online (Sandbox Code Playgroud)
所以我在网上搜索并找到了许多解决方案:
C# 异常。另一个进程
正在使用文件 读取另一个进程正在使用的日志
文件 在 C# 中读取锁定文件的侵入性最小的方法是什么(可能在不安全模式下)?
C#进程无法访问文件''',因为它正在被另一个进程使用
文件正在被另一个进程使用
http://coding.infoconex.com/post/2009/04/21/How-do-I-open -a-file-that-is-in-use-in-C
常见的解决方案是使用using包装FileStream和添加FileShare.ReadWrite.
我尝试了这些解决方案,但我仍然收到另一个进程正在使用该文件的异常。
在我下面的代码中,我打开文件D:\process.log以使用该文件(用于测试),然后尝试打开该文件。
例外是在行:
using (FileStream fileStream = File.Open(i_FileNameAndPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
Run Code Online (Sandbox Code Playgroud)
代码:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
DialogResult dialogResult = openFileDialog.ShowDialog();
if (dialogResult == DialogResult.OK)
{
listView.Items.Clear();
File.Open(@"D:\process.log", FileMode.Open); //make the file being used …Run Code Online (Sandbox Code Playgroud)