我正在开发Winform应用程序,它将员工数据作为4个元素的数组 并使用SEPARATOR(“,”)..
将这些数据正确保存到一行中的文本文件中我的问题是如何让它加载任何行数据并识别分隔符(“,”) 这样我就可以通过名字的第一个项目读取所有数据?
public partial class Form1 : Form
{
string[] data = new string[4];
string name;
string job;
string salary;
string join;
#region Save
void save()
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
{
MessageBox.Show("Please Fill All Fields", "error");
}
FileStream file = new FileStream("info.txt", FileMode.Append, FileAccess.Write);
StreamWriter wr = new StreamWriter(file);
wr.WriteLine(String.Join(",", data));
wr.Flush();
wr.Close();
comboBox1.Items.Add(data[0]);
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
#endregion
#region Search
void search()
{
FileStream file = new FileStream("info.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
sr.ReadLine(string.//what should i do here?
string[] lines = File.ReadAllLines("info.txt");
data[0].CompareTo(comboBox1.SelectedItem);
sr.ReadLine();
if (data[0] == name)
{
textBox1.Text = (data[0]);
textBox2.Text = (data[1]);
textBox3.Text = (data[2]);
textBox4.Text = (data[3]);
}
}
#endregion
答案 0 :(得分:3)
您可以再次阅读该文件,并在您选择的分隔符上使用Split
var textLines = File.ReadAllLines("");
foreach (var line in textLines)
{
string[] dataArray = line.Split(',');
}
答案 1 :(得分:1)
最后工作,行拆分为数组数据, 每个都添加在右侧文本框中:
void search()
{
FileStream file = new FileStream("info.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(file);
sr.ReadLine();
var textLines = File.ReadAllLines("info.txt");
foreach (var line in textLines)
{
string[] dataArray = line.Split(',');
dataArray[0].CompareTo(comboBox1.SelectedItem);
if (dataArray[0] == comboBox1.SelectedItem.ToString())
{
textBox1.Text = (dataArray[0]);
textBox2.Text = (dataArray[1]);
textBox3.Text = (dataArray[2]);
textBox4.Text = (dataArray[3]);
}
}
}
非常感谢李俊伟先生
答案 2 :(得分:0)
string[] sBENCHNO=new Strin[256];
sBENCHNO = ini.IniReadValue("Sextion", "Key");
要调用此功能,请参阅下面的代码
response.xAxis.labels.formatter = function () {
return ("<div title='" + this.value + "'>" + (this.value.length > 25 ? this.value.substring(0, 22) + " .." : this.value) + "</div>");
}