c#读取文件
1.要以对话框的形式读取文件
前提:你要在窗体中加载openfiledialog对话框,才能应用下面的代码,否则出错(加载openfiledialog对话框,在窗体不会体现出来,只是在下面出现而已,要多少
个对话框,就要加载多少个openfiledialog)
代码如下(前提:要在开头加入using System.IO;):
openFileDialog1.AddExtension = true; //增加扩展名
openFileDialog1.Multiselect = true; //多选择文件
openFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
openFileDialog1.Title = "请选择文件";
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
openFileDialog1.ShowDialog(); // 形式:opengfiledialog的名称.showdialog()
string filename = openFileDialog1.FileName; //获取文件的路径
System.IO.StreamReader navReader = new System.IO.StreamReader(filename, System.Text.Encoding.Default);
//System.IO.可以不要
string strLine = navReader.ReadLine(); //ReadLine()按行读取,Readtoend()从头读取到最后一行
int n=0;string[] a = new string[1];char[] charAray = new char[] { ',', ' ', '\t' };
while (strLine != null)
{
a[n] = strLine;
n++;
Array.Resize(ref a, a.Length + 1);
strLine = navReader.ReadLine();
}
navReader.Close(); //关闭读取流
Array.Resize(ref a, a.Length - 1); //因为上面的while中读取到空白时,a的length多加了1,所以要减去
for (int i = 0; i < a.Length;i++ )
{
date = a[ i].Split(charAray, StringSplitOptions.RemoveEmptyEntries); //分别将数字存在数组中
tl.D[i, 0] = (Convert.ToDouble(date[1]));
tl.D[i, 1] = (Convert.ToDouble(date[2])) ;
tl.D[i, 2] = (Convert.ToDouble(date[3])) ;
tl.D[i, 3] = d_h(Convert.ToDouble(date[4]));
tl.D[i , 4] = d_h(Convert.ToDouble(date[5]));
tl.D[i, 5] = d_h(Convert.ToDouble(date[6]));
tl.D[i, 6] = Convert.ToDouble(date[0]);//像片号
//if (tl.D[i, 6] > tl.half)
// tl.D[i, 6] = tl.D[i, 6] ;
//else
// tl.D[i, 6] = tl.D[i, 6] ;
}
private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
//openfile o = new openfile();
////o.MdiParent = this;
//o.Show();
openFileDialog1.AddExtension = true; //增加扩展名
openFileDialog1.Multiselect = true; //多选择文件
openFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
openFileDialog1.Title = "请选择文件";
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
DialogResult dr = openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default);
textBox1.Text = sr.ReadToEnd();
sr.Close();
//string[] a = new string[1];
//int n = 0;
//if (dr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(filename)) ;
//{
//StreamReader sr = new StreamReader(filename,System.Text .Encoding .Default );
//string strLine = sr.ReadLine();
//while (strLine != null)
//{
// a[n] = strLine;
// n++;
// Array.Resize(ref a, a.Length + 1);
// strLine = sr.ReadLine();
//}
//sr.Close();
//Array.Resize(ref a, a.Length - 1);
//sr.Close();
//}
//OpenFileDialog filedialog = new OpenFileDialog();
//filedialog.Multiselect = true;
//filedialog.Title = "请选择文件";
//filedialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
//string filename = openFileDialog1.FileName;
//StreamReader sr = new StreamReader(filename, System.Text.Encoding.Default);
//textBox1.Text = sr.ReadToEnd();
//sr.Close();
}
2.不要对话框
代码如下(前提:要在开头加入using System.IO;):
不要这两行代码(openFileDialog1.ShowDialog(); // 形式:opengfiledialog的名称.showdialog()
string filename = openFileDialog1.FileName; //获取文件的路径),换成 string path; //打开LPS空三文件
path = "F:/实验数据晶/D.txt";
后面的代码相同。
我用到的例子:
链接:https://pan.baidu.com/s/1aYE0-4JuB_GJBV-OIXKwag
提取码:umsj
复制这段内容后打开百度网盘手机App,操作更方便哦