C#writeline:该过程无法访问该文件,因为另一个过程正在使用该文件
我有一个函数,可以在文件夹中读取所有文件.log并提取每个错误行,然后将其写入.CSV文件中。 它可以与小日志文件一起使用,但与600ko这样的“大文件”不使用,然后返回我的错误“该过程无法访问该文件,因为它是由另一个进程使用的。”
我所有的日志文件都在“日志”文件夹中重组。
/*
* Extract data from the log file and right it in a conf.csv file
*/
public void DataExtract(string path)
{
int index = 0;
int nextLine = 0;
int descriptionLine = 0;
string firstLine = "";
string secondLine = "";
string description = "";
try
{
if (!String.IsNullOrEmpty(path))
{
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] logs = System.IO.File.ReadAllLines(path);
string[] confFile = System.IO.File.ReadAllLines(this.confPath);
// read each line of the log file
foreach (string log in logs)
{
if (log.Contains("ERROR"))
{
nextLine = index + 1;
descriptionLine = index + 2;
firstLine = log;
secondLine = logs[nextLine];
string checkDescr = "";
int descNb = descriptionLine + 1;
checkDescr = logs[descNb];
description = logs[descriptionLine];
if (!description.Contains("at"))
{
descriptionLine++;
description = logs[descriptionLine];
}
if (!confFile.Any(s => s.Contains(firstLine)) || !confFile.Any(s => s.Contains(secondLine)))
{
using (StreamWriter sw = File.AppendText(this.confPath))
{
sw.WriteLine(string.Format("{0},{1},{2}", firstLine, secondLine, description));
}
}
index++;
}
Console.WriteLine("Done");
}
}
catch (Exception e)
{
Console.WriteLine("Problem !");
Console.WriteLine(e.Message);
}
}
}
然后,在主要班级中:
string logPath = directoryPath + "\\logs";
string[] logfiles = Directory.GetFiles(logPath, "*.log");
ErrorRecover errorRecover = new ErrorRecover();
// For each log file call the methode for extracting errors logs
foreach (var file in logfiles)
{
Console.WriteLine(file);
errorRecover.DataExtract(file);
}
I have a function that read all my files .log inside a folder and extract each error line and later write it in a .csv file.
It work with small log files but not with "big file" like >600Ko and return me the error "The process cannot access the file because it is being used by another process."
All my log file a regrouped inside a "logs" folder.
/*
* Extract data from the log file and right it in a conf.csv file
*/
public void DataExtract(string path)
{
int index = 0;
int nextLine = 0;
int descriptionLine = 0;
string firstLine = "";
string secondLine = "";
string description = "";
try
{
if (!String.IsNullOrEmpty(path))
{
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] logs = System.IO.File.ReadAllLines(path);
string[] confFile = System.IO.File.ReadAllLines(this.confPath);
// read each line of the log file
foreach (string log in logs)
{
if (log.Contains("ERROR"))
{
nextLine = index + 1;
descriptionLine = index + 2;
firstLine = log;
secondLine = logs[nextLine];
string checkDescr = "";
int descNb = descriptionLine + 1;
checkDescr = logs[descNb];
description = logs[descriptionLine];
if (!description.Contains("at"))
{
descriptionLine++;
description = logs[descriptionLine];
}
if (!confFile.Any(s => s.Contains(firstLine)) || !confFile.Any(s => s.Contains(secondLine)))
{
using (StreamWriter sw = File.AppendText(this.confPath))
{
sw.WriteLine(string.Format("{0},{1},{2}", firstLine, secondLine, description));
}
}
index++;
}
Console.WriteLine("Done");
}
}
catch (Exception e)
{
Console.WriteLine("Problem !");
Console.WriteLine(e.Message);
}
}
}
Then in the Main class i do :
string logPath = directoryPath + "\\logs";
string[] logfiles = Directory.GetFiles(logPath, "*.log");
ErrorRecover errorRecover = new ErrorRecover();
// For each log file call the methode for extracting errors logs
foreach (var file in logfiles)
{
Console.WriteLine(file);
errorRecover.DataExtract(file);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,根据评论以及我了解的内容,我尝试首先恢复所有日志,然后将其存储在列表中,然后从列表中写入所有日志中,然后将所有日志写入conf.csv文件中。
我想这不是有史以来最好的代码,但现在可以使用,即使有大文件,我也没有错误。
So according to comments and what I understand I tried to first recover all my logs and store it inside a list and then from the list I write all in my conf.csv file.
I guess it's not the best code ever but it work for now and I don't have the error even with big files.
获取使用此方法锁定文件的流程:
然后杀死:
完成;
Get processes who locking file with this method:
then kill that:
Done;