我需要读取可能被锁定的Windows文件,但我不想创建任何类型的锁,以防止其他进程写入该文件.
另外,即使文件被锁定以供独家使用,我也想看看里面有什么.
虽然这不是我的确切用例,但请考虑如何在使用和装载时读取SQL/Exchange日志或数据库文件.我不想导致腐败,但我仍然希望看到文件的内部并阅读它.
目标
我想按下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) 我有一个大致这样做的程序:
问题是,在从第4步到第1步之后,它无法读取文件说它正被另一个进程使用.
我收到错误:
未处理的异常:System.IO.IOException:进程无法访问文件'c:\ test.xml',因为它正由另一个进程使用.
什么出错了?我的程序的第1步的读者是否仍然打开文件,或者是访问该文件的完全不同的进程,还是filewatcher在从4移动到步骤1之后仍在观看文件,尽管将标志设置为false?
我正在玩C#,我遇到了一个问题.当我尝试创建一个新文件时,程序会断开并说该文件正由另一个进程使用.这可能是我忽略的愚蠢,但我无法弄明白!
这是我的代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace myNameSpace
{
public partial class MainWindow : Form
{
public static string folderPath = @"fullpath";
public MainWindow()
{
InitializeComponent();
StoredTb.Text = folderPath;
String[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
myDropDown.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void myDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke(new MethodInvoker(myDropDown_invokedMethod));
}
private void myDropDown_invokedMethod()
{
string fullpath = MainWindow.folderPath + myDropDown.SelectedText + ".txt";
StreamReader sr = new …Run Code Online (Sandbox Code Playgroud) 代码片段只是应该将字符串写入名为“all_results.txt”的文本文件中。我在 File.WriteAllText 中实现时出错。在网上搜索解决方案后,我尝试使用 FileStream 和 StreamWriter 作为替代品。问题仍然存在。
它给了我:
IOException 未处理:进程无法访问文件“C:\Users\MadDebater\Desktop\ConsoleTest1\ConsoleTest\bin\Debug\all_results.txt”,因为它正在被另一个进程使用。
奇怪的是,错误是任意发生的。它可能是在第 3 次循环期间或第 45 次循环期间发生错误。我提供了该类的完整代码,以防问题比看起来更严重。我确信这与我的病毒扫描程序或类似的东西无关。
try
{
using (FileStream stream = new FileStream(@"all_results.txt", FileMode.Create)) // Exception here
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(result);
writer.Dispose();
writer.Close();
}
stream.Dispose();
stream.Close();
}
}
catch (IOException ex)
{
Console.WriteLine(ex);
}
Run Code Online (Sandbox Code Playgroud)
即使我尝试这样做,它仍然失败。
try
{
File.WriteAllText(@"all_results.txt", result); // Exception here
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)
以下是该类的完整代码。它的目的是接收 Twitter 推文列表,并使用贝叶斯分类将它们一一分类。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BayesClassifier;
using …Run Code Online (Sandbox Code Playgroud)