C# 文件的保存与读取
2016-05-22 02:25
561 查看
1.保存文件到指定的textBox控件中
2.文件另存为到用户指定的txt文件
3.读取txt文件到textBox中
textBox3.Text = File.ReadAllText(“D:\1.txt”);
4.读取指定的txt文件到指定的textBox中
未完成DEMO
StreamWriter sw = new StreamWriter( "D:\\1.txt"); sw.WriteLine(this.textBox1.Text); sw.Flush();//文件流 sw.Close();//最后要关闭写入状态
2.文件另存为到用户指定的txt文件
Stream myStream; //新建另存为对话框 SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //指定保存文件类型 这里为txt saveFileDialog1.Filter = "txt files (*.txt)|*.txt"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { using (StreamWriter sw = new StreamWriter(myStream)) { sw.WriteLine(this.textBox1.Text); } myStream.Close(); MessageBox.Show("Saved"); } } } }
3.读取txt文件到textBox中
textBox3.Text = File.ReadAllText(“D:\1.txt”);
4.读取指定的txt文件到指定的textBox中
这里写代码片
未完成DEMO
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter( "D:\\1.txt"); sw.WriteLine(this.textBox1.Text); sw.Flush();//文件流 sw.Close();//最后要关闭写入状态 } private void button3_Click(object sender, EventArgs e) { // StreamReader sd = File.OpenText("D:\\1.txt"); // sd.ReadToEnd("D:\\1.txt", System.Text.Encoding.UTF8); textBox3.Text = File.ReadAllText("D:\\1.txt"); } private void button4_Click(object sender, EventArgs e) { Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { using (StreamWriter sw = new StreamWriter(myStream)) { sw.WriteLine(this.textBox1.Text); } myStream.Close(); MessageBox.Show("Saved"); } } } private void button5_Click(object sender, EventArgs e) { string v_OpenFolderPath = @"目录路径"; System.Diagnostics.Process.Start("explorer.exe", v_OpenFolderPath); } private void button6_Click(object sender, EventArgs e) { Stream s; OpenFileDialog op = new OpenFileDialog(); op.Filter = "txt files (*.txt)|*.txt"; op.FilterIndex = 2; op.RestoreDirectory = true; if (op.ShowDialog() == DialogResult.OK) { if ((s = op.OpenFile()) != null) { textBox3.Text = File.ReadAllText(""); } } } } }
相关文章推荐
- C#超时处理
- C#委托举例
- C#事件与委托详解【精华 多看看】
- C#基础-邮件发送
- c# 极验验证识别
- 【算法相关】高频词汇统计系统(二)
- C# 带权重的随机 与 不带权重的随机
- C# UDP服务编程(NativeErrorCode = 10054)
- C#开源大全项目
- C# dbf文件在dataGirdView控件中显示方法
- C#【爬虫】明星 微博+贴吧+格式化+发送
- '用Roslynpad做一个轻量级的C#编辑器'
- C# split字符串
- C# winform windows服务安装与卸载
- C# Log4Net使用示例
- 对数组的每个元素仍是数组的遍历方法,c#,foreach,for
- CLR Via C# 第五章(基元类型,引用类型与值类型)(1)——读书笔记
- C#—Windows应用基础2
- C#中系统时间和UNIX时间戳互相转换
- C#操作XML