C#从txt文件读取数据
目录
第一步新建txt文件,写入内容
第二步读取数据
最终效果图
第一步新建txt文件,写入内容
我是放在D盘下的,数据以逗号隔开的,是英文逗号
第二步读取数据
在需要读取数据的页面,添加代码,就可以了
注意
- 这里的Phone_Load事件一定要加载上,如果没加载,不会报错,也不会出效果
- 如果文件里有中文的话,可能会乱码,可以看情况把 GB2312 换成 UTF-8
private void Phone_Load(object sender, EventArgs e)
{
string ReadLine;
string[] array;
string Path = @"D:\FrontierApp.TXT";
StreamReader reader = new StreamReader(Path,System.Text.Encoding.GetEncoding("GB2312"));
while (reader.Peek() >= 0)
{
try
{
ReadLine = reader.ReadLine();
if (ReadLine != "")
{
ReadLine = ReadLine.Replace("\"", "");
array = ReadLine.Split(',');
if (array.Length == 0)
{
MessageBox.Show("您选择的导入数据类型有误,请重试!");
return;
}
for (int i = 0; i < array.Length; i++)
{
comboBox2.Items.Add(array[i]);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
最终效果图
C# 萌新真的太不容易了,嘤嘤 ╥﹏╥…
还没有评论,来说两句吧...