我正在嘗試閱讀和使用 txt 檔案中的所有行。使用一種方法,我遍歷它們 asinc,并嘗試獲取資料。我的問題是,輸出看起來只包含第一個 txt 檔案中的資料。我只是找不到問題所在。我將不勝感激任何幫助。
這是我的代碼:
string[] files = Directory.GetFiles("C:/DPS-EDPWB05/forlogsearch", "*", SearchOption.AllDirectories);
//data I need from the txts
List<string> num = new List<string>();
List<string> date = new List<string>();
List<string> time = new List<string>();
List<string> sip = new List<string>();
List<string> csmethod = new List<string>();
List<string> csuristem = new List<string>();
List<string> csuriquery = new List<string>();
List<string> sport = new List<string>();
List<string> csusername = new List<string>();
List<string> cip = new List<string>();
List<string> csuseragent = new List<string>();
List<string> csreferer = new List<string>();
List<string> scstatus = new List<string>();
List<string> scsubstatus = new List<string>();
List<string> cswin32status = new List<string>();
List<string> timetaken = new List<string>();
int x = 0;
int y = 0;
int i = 0;
int filesCount = 0;
string v = "";
//Taking the data from the Log, getting a list of string[]
//items with the lines from the txts
List<string[]> lines = new List<string[]>();
while (i < files.Length)
{
lines.Add(ReadAllLinesAsync(files[i]).Result);
i ;
}
//Trying to get the data from the string[]s
do
{
string line;
int f = 0;
string[] linesOfTxt = lines[filesCount];
do
{
line = linesOfTxt[f];
string[] splittedLine = { };
splittedLine = line.Split(' ', 15, StringSplitOptions.None);
y = splittedLine.Count();
if (y == 15)
{
num.Add(x.ToString());
date.Add(splittedLine[0]);
time.Add(splittedLine[1]);
sip.Add(splittedLine[2]);
csmethod.Add(splittedLine[3]);
csuristem.Add(splittedLine[4]);
csuriquery.Add(splittedLine[5]);
sport.Add(splittedLine[6]);
csusername.Add(splittedLine[7]);
cip.Add(splittedLine[8]);
csuseragent.Add(splittedLine[9]);
csreferer.Add(splittedLine[10]);
scstatus.Add(splittedLine[11]);
scsubstatus.Add(splittedLine[12]);
cswin32status.Add(splittedLine[13]);
timetaken.Add(splittedLine[14]);
x ;
}
f ;
} while (f < linesOfTxt.Length);
filesCount ;
}
while (filesCount < files.Count());
畢竟,我對這些和東西進行了分組,但這是在我需要的資料串列被填滿之后發生的——所以問題一定出在某個地方。另外,我的 asinc 閱讀器(我在 stackoverflow 上找到):
public static Task<string[]> ReadAllLinesAsync(string path)
{
return ReadAllLinesAsync(path, Encoding.UTF8);
}
public static async Task<string[]> ReadAllLinesAsync(string path, Encoding encoding)
{
var lines = new List<string>();
// Open the FileStream with the same FileMode, FileAccess
// and FileShare as a call to File.OpenText would've done.
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, DefaultBufferSize, DefaultOptions))
using (var reader = new StreamReader(stream, encoding))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
lines.Add(line);
}
}
return lines.ToArray();
}
uj5u.com熱心網友回復:
示例代碼有幾個問題,我不確定是什么導致了實際問題。
- 無需撰寫自己的 ReadAllLinesAsync,只需使用File.ReadAllLinesAsync
- 您通常應該避免,
.Result因為這是一個阻塞操作,這有可能導致死鎖。在這種情況下,您應該只呼叫同步版本File.ReadAllLines,而不是。 - 如果可能,請使用foreach或 for 回圈。它們使您更容易查看您的代碼是否正確,并避免將回圈邏輯分布在多行上。
- 據我所見,您正在以相同的方式處理每一行,因此您應該能夠通過使用
List<string> lines和AddRange合并所有檔案中的所有行 - 一種更常見的方法是存盤一個具有 15 個不同屬性的類的物件串列,而不是保留 15 個不同的屬性串列。
- 每當您需要存盤資料時,您應該認真考慮使用現有的序列化格式,如 json、xml、csv、protobuf 等。這使您可以使用現有的、經過良好測驗的庫來寫入和讀取資料并將其轉換為您自己的型別。
uj5u.com熱心網友回復:
我接受了@JonasH 的答案作為解決方案,因為它修復了我寫得不好的代碼中的很多東西。
我犯的錯誤更加菜鳥 - 我沒有提供正確的檔案路徑。一切正常,當我在很多日志檔案上使用它時,它只需要很多時間 - 現在我會根據你的建議改進它。非常感謝您的幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407567.html
標籤:
