我想要做的实际上是让我的程序打开并设置一个字符串数组,以便我可以将它用于 if 命令(它用于阻止人们)并且我想在其中存储另一个字符串以便可以保存它并且仍然在程序重新启动时使用。有人可以告诉我如何做到这一点吗?谢谢,麻烦您了 :]
问问题
1351 次
2 回答
0
You could use the following:
string[] lines = File.ReadAllLines(@"Path here").ToArray();
This splits each line up into the array.
As for saving to file, google "saving to file in c#", you will get plenty of results and tutorials.
于 2013-04-14T03:02:23.550 回答
0
这绝对比我首先发布的解决方案更好:
File.WriteAllLines(fileName, yourStringArray);
yourStringArray = File.ReadAllLines(fileName);
下面是我的第一个答案。正确,但是废话!
像这样的东西应该适合你。不要怪我不准确,我还没有完全清醒!;)
{
string fileName = @"d:\temp\blacklist.txt";
char seperator = ';';
public Form1()
{
InitializeComponent();
string[] users = { "Dave", "John", "Shawn" };
//Save(users);
users = Load();
}
public string[] Load()
{
string line;
using (StreamReader sr = new StreamReader(this.fileName))
{
line = sr.ReadToEnd();
}
return line.Split(seperator);
}
public void Save(string[] users)
{
using (StreamWriter sw = new StreamWriter(this.fileName))
{
string line = string.Empty;
foreach (string user in users)
{
line += string.Format("{0}{1}", user, seperator);
}
sw.WriteLine(line);
sw.Flush();
}
}
}
于 2013-04-14T04:49:43.410 回答