c#读取txt文件“:”前面的内容和后面的内容
例如以下文件"1.txt"1:0.1,0.3,0.52:1.6,1.7,1.83:3.6,3.7,3.8读取txt文件,某个组合框选定冒号":"前的1,则三个文本框显示后...
例如以下文件"1.txt"
1:0.1,0.3,0.5
2:1.6,1.7,1.8
3:3.6,3.7,3.8
读取txt文件,某个组合框选定冒号":"前的1,则三个文本框显示后面三个数据,textbox1显示0.1,textbox2显示0.3,选择2则显示第二行后面三个数据。。。
求代码,谢谢! 展开
1:0.1,0.3,0.5
2:1.6,1.7,1.8
3:3.6,3.7,3.8
读取txt文件,某个组合框选定冒号":"前的1,则三个文本框显示后面三个数据,textbox1显示0.1,textbox2显示0.3,选择2则显示第二行后面三个数据。。。
求代码,谢谢! 展开
4个回答
展开全部
public partial class Form1 : Form
{
Dictionary<string, string[]> dic = new Dictionary<string, string[]>();
List<string> listKeys = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<string> list_Get = Read(Application.StartupPath+"\\1.txt");
foreach (string s in list_Get) {
string[] arr = s.Split(':');
listKeys.Add(arr[0]);
string[] arr_value = arr[1].Split(',');
dic.Add(arr[0],arr_value);
}
this.comboBox1.DataSource = listKeys;
}
public List<string> Read(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
string line;
List<string> list = new List<string>();
while ((line = sr.ReadLine()) != null)
{
list.Add(line.ToString());
}
return list;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string key = comboBox1.SelectedItem.ToString();
this.textBox1.Text = dic[key][0];
this.textBox2.Text = dic[key][1];
this.textBox3.Text = dic[key][2];
}
}
{
Dictionary<string, string[]> dic = new Dictionary<string, string[]>();
List<string> listKeys = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<string> list_Get = Read(Application.StartupPath+"\\1.txt");
foreach (string s in list_Get) {
string[] arr = s.Split(':');
listKeys.Add(arr[0]);
string[] arr_value = arr[1].Split(',');
dic.Add(arr[0],arr_value);
}
this.comboBox1.DataSource = listKeys;
}
public List<string> Read(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
string line;
List<string> list = new List<string>();
while ((line = sr.ReadLine()) != null)
{
list.Add(line.ToString());
}
return list;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string key = comboBox1.SelectedItem.ToString();
this.textBox1.Text = dic[key][0];
this.textBox2.Text = dic[key][1];
this.textBox3.Text = dic[key][2];
}
}
展开全部
private void button1_Click(object sender, EventArgs e)
{
StreamReader streamReader = new StreamReader(@"C:\test.txt");
Dictionary<string, string> dic = new Dictionary<string, string>();
List<string> list = new List<string>();
string s;
while ((s = streamReader.ReadLine()) != null)
{
var temp = s.Split(new char[] { ':' });
dic.Add(temp[0], temp[1]);
}
string value = comboBox1.Text;
if (dic.ContainsKey(value))
{
string[] result = dic[value].Split(new char[] { ',' });
txt1.Text = result[0];
txt2.Text = result[1];
txt3.Text = result[2];
}
}
{
StreamReader streamReader = new StreamReader(@"C:\test.txt");
Dictionary<string, string> dic = new Dictionary<string, string>();
List<string> list = new List<string>();
string s;
while ((s = streamReader.ReadLine()) != null)
{
var temp = s.Split(new char[] { ':' });
dic.Add(temp[0], temp[1]);
}
string value = comboBox1.Text;
if (dic.ContainsKey(value))
{
string[] result = dic[value].Split(new char[] { ',' });
txt1.Text = result[0];
txt2.Text = result[1];
txt3.Text = result[2];
}
}
追问
一楼回答很到位,可是如果不用button读取txt,那代码应该怎么写?
追答
嗯,那您继续找楼上的看吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你一个思路:
string aLine = null;
StringReader strReader = new StringReader("文件路径");
while(true)
{
aLine = strReader.ReadLine();
if(aLine != null)
{
//分析行内容,可以用正则表达式,也可以先用 “:”分割,在用“,”分割获取每一项
}
else
{
break;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-10-15
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询