C#Winform自学笔记第十篇,上接(九)
内容一览
- 文件的读写
——————正文——————
(一)文件的读写
通过FileStream打开文本文件、写入文本文件、设置文件属性、实施对文件的目录操作管理。
实践案例:
本次实验目标是通过一个窗体,在单击相应按钮控件时,可以完成对文件的读写操作、磁盘操作以及对目录的管理操作。
1)从工具箱拖拽Button控件、RichTextBox、ComboBox和GroupBox控件,排布如下图所示:
2)对各按钮的代码编辑如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace UNIT3_1
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
}
//写入文件操作
private void button2_Click(object sender, EventArgs e)
{
int p = comboBox1.SelectedIndex;
if(p==-1)
{
MessageBox.Show("请选择文件写入方式:", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string filecontent = richTextBox1.Text.Trim();
MyFileOption myoption = new MyFileOption();
string filepath = @"E:\\1.txt";
bool i = myoption.WriteTextFile(filepath, filecontent, Convert.ToInt16(comboBox1.SelectedIndex));
if(i==true)
{
MessageBox.Show("保存成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("写入文件时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
//磁盘读写按钮
private void button4_Click(object sender, EventArgs e)
{
Int16 p = Convert.ToInt16(comboBox2.SelectedIndex);
if(p==-1)
{
MessageBox.Show("请您选择磁盘文件操作方式", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sourcepath = "E:\\1.txt";
string targetpath = "E:\\2.txt";
MyFileOption myoption = new MyFileOption();
bool i = myoption.DiskFileOption(sourcepath, targetpath, p);
if(i==true)
{
MessageBox.Show("磁盘文件操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("操作文件操作时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
//读出文件按钮
private void button3_Click(object sender, EventArgs e)
{
MyFileOption myoption = new MyFileOption();
string filepath = @"E:\\1.txt";
Int16 i = 0;
string filecontent = " ";
myoption.ReadTextFile(filepath, out i, out filecontent);
if(i==0)
{
MessageBox.Show(filecontent, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}else if(i==2)
{
richTextBox1.Text = filecontent;
MessageBox.Show("读取文件成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//关闭按钮
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = null;
richTextBox1.Focus();
}
//文件属性确认按钮
private void button5_Click(object sender, EventArgs e)
{
string filepath = @"E:\\1.txt";
if(checkBox1.Checked&&checkBox2.Checked)
{
File.SetAttributes(filepath, FileAttributes.ReadOnly | FileAttributes.Hidden);
MessageBox.Show("文件已经改为只读且隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if(!checkBox1.Checked&&!checkBox2.Checked)
{
File.SetAttributes(filepath, FileAttributes.Archive);
MessageBox.Show("文件已经改为正常", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if(checkBox2.Checked)
{
File.SetAttributes(filepath, FileAttributes.ReadOnly);
MessageBox.Show("文件已经改为只读", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
if(checkBox1.Checked)
{
File.SetAttributes(filepath, FileAttributes.Hidden);
MessageBox.Show("文件已经改为隐藏", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
//文件目录操作
private void button6_Click(object sender, EventArgs e)
{
Int16 p = Convert.ToInt16(comboBox3.SelectedIndex);
if(p==-1)
{
MessageBox.Show("请您选择文件夹操作方式", "警告信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string sourcepath = @"E:\\1.txt";
string targetpath = @"E:\\2.txt";
MyFileOption myoption = new MyFileOption();
string[] filename = null;
bool i = myoption.DirectoryOption(sourcepath, targetpath, p, out filename);
if(i==true)
{
MessageBox.Show("磁盘文件夹操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
if(filename!=null)
{
foreach(string somestring in filename)
{
richTextBox1.Text += somestring + "\r\n";
}
}
}
else
{
MessageBox.Show("磁盘文件夹操作时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}
3)需要在项目中新建一个类,在项目中添加一个FIleOption.cs的类文件,并对类文件的各种方法进行编辑(这才是这个实验的核心功能),代码如下:
编辑类文件代码的时候也需添加头文件using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace UNIT3_1
{
class MyFileOption
{
public bool WriteTextFile(string filepath,string filecontent,Int16 WriteMethord)
{
bool i =true;
try{
if(WriteMethord==0)
{
FileStream textfile=File.Open(filepath,FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter sw=new StreamWriter(textfile,Encoding.Default);
sw.Write(filecontent);
i=true;
sw.Close();
textfile.Close();
}
else if(WriteMethord==1)
{
FileStream textfile =File.Open(filepath,FileMode.Append,FileAccess.Write);
StreamWriter sw = new StreamWriter(textfile,Encoding.Default);
sw.Write(filecontent);
i=true;
sw.Close();
textfile.Close();
}
else if(WriteMethord==2)
{
FileStream textfile=File.Open(filepath,FileMode.Create,FileAccess.Write);
StreamWriter sw= new StreamWriter(textfile,Encoding.Default);
sw.Write(filecontent);
i=true;
sw.Close();
textfile.Close();
}
return i;
}
catch{
i=false;
return i;
}
}
public bool DiskFileOption(string SourcePath,string TargetPath,Int16 OptionMethord)
{
bool i = true;
try
{
if(OptionMethord==0)
{
FileStream textfile = File.Create(SourcePath);
textfile.Close();
}
else if(OptionMethord==1)
{
File.Delete(SourcePath);
}
else if(OptionMethord==2)
{
File.Copy(SourcePath, TargetPath, true);
}
else if(OptionMethord==3)
{
File.Move(SourcePath, TargetPath);
}
return i;
}
catch
{
i = false;
return i;
}
}
public void ReadTextFile(string filepath,out Int16 i,out string filecontent)
{
if(File.Exists(filepath))
{
try
{
StreamReader textreader = new StreamReader(filepath, System.Text.Encoding.Default);
filecontent = textreader.ReadToEnd();
textreader.Close();
i = 1;
}
catch
{
i = 2;
filecontent = "文件读取错误";
}
}
else
{
i = 0;
filecontent = "文件或路径无效!";
}
}
public bool DirectoryOption(string Directorypath,string TargetDirectorypath,Int16 OptionMethord,out string[] filename)
{
bool k = true;
filename = null;
if(Directory.Exists(Directorypath))
{
try
{
if(OptionMethord==0)
{
Directory.CreateDirectory(Directorypath);
}
else if(OptionMethord==1)
{
Directory.Delete(Directorypath, true);
}
else if(OptionMethord==3)
{
filename = Directory.GetFiles(Directorypath);
}
}
catch
{
k = false;
}
}
else
{
Directory.CreateDirectory(Directorypath);
k = true;
}
return k;
}
}
}
实验结果:
①先在richtextBox中写入要写入文件的内容:然后选择写入类型,再点击“写入文件”