C#打开并读取已有文件中的数值
private static string[] info = new string[5]; // 设定信息
private void OpenExistedFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel文件(*.csv;)|*.csv"; //此处可以定义任意文件
ofd.ValidateNames = true;
ofd.CheckPathExists = true;
ofd.CheckFileExists = true;
ofd.InitialDirectory = GlobalData.saveToPath;
string[] temp = new string[2];
try
{
if (ofd.ShowDialog() == DialogResult.OK)
{
string strFileName = ofd.FileName;
info = ReadCSV(strFileName, info);
}
for(int i = 0; i < 24; i++)
{
temp = info[i].Split(',');
info[i] = temp[1]; // 读取的数据放入info数组中
}
}
catch
{
return;
}
}
本文介绍了一种使用C#语言打开并读取CSV文件的方法。通过实例代码展示了如何使用OpenFileDialog选择文件,以及如何读取选定的CSV文件并将数据存储到数组中。

9115

被折叠的 条评论
为什么被折叠?



