C#之文件的读写(一)
整个系统的界面如下:
先上代码:
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 WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private static string directory_path = @"E:\study\code\C#\winform\2017-12-9\WindowsFormsApp1\1"; private static string directory_otherpath = @"E:\study\code\C#\winform\2017-12-9"; private static string directory_anotherpath = "E:\\study\\code\\C#\\winform\\2017-12-9\\WindowsFormsApp1\\"; //private static string directory_vv = "E:\\study\\code\\C#\\winform\\2017-12-9\\WindowsFormsApp1\\12.txt"; private void groupBox1_Enter(object sender, EventArgs e) { } /// <summary> /// 创建目录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { try { Directory.CreateDirectory(directory_path); button1.Enabled = false; button2.Enabled = true; button3.Enabled = true; button1.Enabled = true; MessageBox.Show("文件夹成功建立","提示!"); } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 删除目录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { try { Directory.Delete(directory_path); button2.Enabled = false; button1.Enabled = true; button3.Enabled = false; button4.Enabled = false; MessageBox.Show("文件夹删除成功","提示!"); } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 移动目录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { try { Directory.Move(directory_path, directory_otherpath); MessageBox.Show("文件夹移动成功", "提示!"); } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 目录创建时间 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button4_Click(object sender, EventArgs e) { try { MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)),"提示!"); } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 创建文本文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) { try { if (textBox1.Text.Length == 0) MessageBox.Show("文件名禁止为空", "提示!"); else { directory_anotherpath = directory_anotherpath + textBox1.Text.Trim() + ".txt"; StreamWriter sw = File.CreateText(directory_anotherpath); richTextBox1.Enabled = true; MessageBox.Show("文件创建成功。", "提示!"); sw.Close(); } } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 打开文本文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button7_Click(object sender, EventArgs e) { try { OpenFileDialog open = new OpenFileDialog(); open.Title = "打开文本文件"; open.FileName = ""; open.AddExtension = true; open.CheckFileExists = true; open.CheckPathExists = true; open.Filter = "文本文件(*.txt) | *.txt"; open.ValidateNames = true; if (open.ShowDialog() == DialogResult.OK) { StreamReader sr = new StreamReader(open.FileName,System.Text.Encoding.Default); this.richTextBox1.Text = sr.ReadToEnd(); MessageBox.Show("文件打开成功","提示!"); richTextBox1.Enabled = true; sr.Close(); } } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } /// <summary> /// 保存编辑文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { try { FileStream textfile = File.Open(directory_anotherpath, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(textfile, Encoding.GetEncoding("GB2312")); sw.WriteLine(richTextBox1.Text.ToString()); MessageBox.Show("文件写成功","提示!"); sw.Close(); } catch (Exception mm) { MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "提示!"); } } } }
分析:
分析代码进行窗口调试调整的时候,用try catch finally 组合会使得整个程序不会出现致命性异常。
创建目录:
使用directory类的创建目录方法的功能。
directory_path 是自己设定的文件路径,注意有@和没@得区别。
删除目录和移动目录应用的同样是Directory类中的方法。
目录创建时间:
MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)),"提示!");
Format是String类中的一个格式化数据的一个方法。
创建文本文件:
CreateText(string FilePath) 方法是创建或打开一个文件,用于写入UTF-8 编码的文件
File.CreateText 返回的一个StreamWriter对象。使用完要关闭。
sw.Close();
打开文本文件:
OpenFileDialog 是创建一个打开的对话框
其中要设置对话框的属性:
基本属性设置。
StreamReader sr = new StreamReader(open.FileName,System.Text.Encoding.Default); this.richTextBox1.Text = sr.ReadToEnd();
fileName是选择后的文件打开路径,Encoding.Default是获取操作系统当前ANSI代码页的编码。(ANSI代码是一种字符代码)
打开读取成功后要记得关闭StreamReader的对象。
保存编辑文件:
FileStream textfile = File.Open(directory_anotherpath, FileMode.OpenOrCreate, FileAccess.Write);
设置读写模式和访问权限的流。
定义读的流对象,并将FileStream对象写入StreamReader对象中,并设置格式。
保存成功后要关闭。