C#打开保存txt文件
2017-09-14 15:19
225 查看
using System.IO;
OpenFileDialog
StreamReader
if (textBox_FileName.Text == "")
{
MessageBox.Show("请先打开文件!");
return;
}
st = new StreamReader(textBox_FileName.Text, Encoding.GetEncoding("gb2312"));
string str = st.ReadLine();
richTextBox1.Text = "";
while (str != null)
{
string[] temp;
if (str!="")
{
temp = str.Split(',');
if (temp.Length != 2)
{
MessageBox.Show("txt中经纬度格式有误,每行一组数据,以英文逗号隔开");
return;
}
if (Convert.ToDouble(temp[0]) > 90.0 || Convert.ToDouble(temp[1]) > 180.0)
{
MessageBox.Show("经纬度BL不在合理的范围内");
return;
}
richTextBox1.AppendText(str + "\n");
BL2XY(Convert.ToDouble(temp[0]), Convert.ToDouble(temp[1]));
richTextBox1.AppendText("\n");
}
str = st.ReadLine();
}
save_file("BL2XY");
SaveFileDialog
public void save_file(string zhengSuanOrfanSuan)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = textBox_FileName.Text + "_" + zhengSuanOrfanSuan;
saveFileDialog1.Filter = "txt files(*.txt)|*.txt|All Files(*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if(dr==DialogResult.OK&&saveFileDialog1.FileName.Length>0)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show("存储文件成功", "保存文件");
}
}
转:C#用openfiledialog文件和savefileDialog打开和保存文件
OpenFileDialog
Stream mystream=null; OpenFileDialog FileDialog1 = new OpenFileDialog(); FileDialog1.Filter = "txt files(*.txt)|*.txt|All Files(*.*)|*.*"; if(FileDialog1.ShowDialog()==DialogResult.OK) { try { if((mystream=FileDialog1.OpenFile())!=null) { st=new StreamReader(FileDialog1.FileName,Encoding.GetEncoding("gb2312")); string str=st.ReadLine(); richTextBox1.Text = ""; while(str!=null) { richTextBox1.AppendText(str + "\n"); str=st.ReadLine(); } richTextBox1.AppendText("\n"); } } catch(Exception ex) { MessageBox.Show("打开文件出错:"+ex.Message); } textBox_FileName.Text = FileDialog1.FileName; }
StreamReader
if (textBox_FileName.Text == "")
{
MessageBox.Show("请先打开文件!");
return;
}
st = new StreamReader(textBox_FileName.Text, Encoding.GetEncoding("gb2312"));
string str = st.ReadLine();
richTextBox1.Text = "";
while (str != null)
{
string[] temp;
if (str!="")
{
temp = str.Split(',');
if (temp.Length != 2)
{
MessageBox.Show("txt中经纬度格式有误,每行一组数据,以英文逗号隔开");
return;
}
if (Convert.ToDouble(temp[0]) > 90.0 || Convert.ToDouble(temp[1]) > 180.0)
{
MessageBox.Show("经纬度BL不在合理的范围内");
return;
}
richTextBox1.AppendText(str + "\n");
BL2XY(Convert.ToDouble(temp[0]), Convert.ToDouble(temp[1]));
richTextBox1.AppendText("\n");
}
str = st.ReadLine();
}
save_file("BL2XY");
SaveFileDialog
public void save_file(string zhengSuanOrfanSuan)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.FileName = textBox_FileName.Text + "_" + zhengSuanOrfanSuan;
saveFileDialog1.Filter = "txt files(*.txt)|*.txt|All Files(*.*)|*.*";
saveFileDialog1.RestoreDirectory = true;
DialogResult dr = saveFileDialog1.ShowDialog();
if(dr==DialogResult.OK&&saveFileDialog1.FileName.Length>0)
{
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show("存储文件成功", "保存文件");
}
}
转:C#用openfiledialog文件和savefileDialog打开和保存文件
相关文章推荐
- C#读写文件|遍历文件|打开保存文件
- C# 打开和保存文件对话框
- 解决ubuntu打开windows下保存的txt文件中文乱码问题
- C# 使用打开保存文件对话框
- C# 保存 byte 数组 到文件中, 用vs2005 打开该文件查看
- C#用openfiledialog文件和savefileDialog打开和保存文件
- C#读写文件、遍历文件、打开保存文件,窗体程序
- C#按键打开文件选择对话框,并把选择好的路径保存/显示到textBox
- C#学习笔记(十六):使用打开保存文件对话框
- C#项目打开/保存文件夹/指定类型文件,获取路径
- C#打开/保存文件,获取路径
- C#项目打开/保存文件夹/指定类型文件,获取路径
- C#中打开文件、目录、保存窗口
- C#在线打开编辑保存Excel文件[pageoffice]
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- C#读写文件、遍历文件、打开保存文件,窗体程序
- C#学习笔记(十六):使用打开保存文件对话框
- opencv打开matlab保存的txt文件,并赋值为Mat矩阵
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- C#学习笔记(十六):使用打开保存文件对话框