C# 文件的保存与读取
来源:互联网 发布:酒店前台收银软件 编辑:程序博客网 时间:2024/05/24 03:16
1.保存文件到指定的textBox控件中
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(""); } } } }}
0 0
- C# 文件的保存与读取
- C# 文件读取与保存
- C#配置项的保存与读取
- Android----文件的保存与读取
- Android---02---文件的保存与读取
- 实例教程三:文件的保存与读取
- android文件的保存与读取
- android---文件的保存与读取
- json文件的读取与保存
- Android文件的读取与保存
- android文件的保存与读取
- Android--文件保存与读取
- Android 文件保存与读取
- 4.文件保存与读取
- Android--文件保存与读取
- Android文件保存与读取
- c++文件保存与读取
- C#保存文件、读取文件对话框
- [leetcode] 【数组】1. Two Sum
- cp: cannot create symbolic link XXX Operation not permitted
- Struts2请求参数扩展名的配置
- python with
- HTML5的一些术语和概念
- C# 文件的保存与读取
- [leetcode] 【数组】15. 3Sum
- Ubuntu(Linux) 下 unzip 命令使用详解
- [leetcode] 【数组】16. 3Sum Closest
- 微信开发获取code
- grunt的安装使用
- 将项目改为MRC的方法(有图有真相)
- 关于js传送json到.net后台处理
- 自定义控件