的config.txt(內容)讀行從一個文本文件和排序成獨立的變量
databaseHost = localhost;
databaseUsername = root;
databasePassword = root;
databaseName = test;
C#代碼
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("config.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
Console.WriteLine(line); // Write to console.
}
}
Console.ReadKey();
這目前打印出的文本文件中的一切。 我想將文本存儲到公共變量中(我已經聲明)。我曾嘗試使用String.Split。我想我錯了,但是如果這裏有人能帶領我走向正確的方向,我會非常感激。我還在學習,我很年輕!
我取得了一些進展: 繼承人我的新C#文件
char[] equals = { '=' };
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader("config.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line); // Add to list.
string[] linr = line.Split(equals);
Console.WriteLine(linr[1]); // Write to console.
}
}
Console.ReadKey();
}
,並將其輸出
localhost;
root;
root;
test;
進行的很好。
,如果你發佈你的文本文件的內容,這將有助於。此外,如果這是一種標準的CSV文件格式,現有工具(搜索NuGet.org)可以爲您解析。 – Igor
''我試過使用String.Split。「 - 不是根據你沒有的問題中的代碼。你究竟嘗試了什麼,它以什麼方式不產生期望的結果? – David
我不知道如何正確地做到這一點。抱歉。 –