前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3-4 文件读写例子(2)

3-4 文件读写例子(2)

作者头像
py3study
发布2020-01-14 15:51:45
3560
发布2020-01-14 15:51:45
举报
文章被收录于专栏:python3python3

//=========================第一部分:主界面功能设计============================= using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO;   namespace FileOptionApplication {     public partial class Form6 : Form     {         public Form6()         {             InitializeComponent();         }         /// <summary>         /// 读写文件操作         /// </summary>         private void button3_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 = @"c:\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);                 }             }         }         /// <summary>         /// 文件磁盘操作         /// </summary>         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 = "c:\\1.txt";                 string targetpath = "c:\\2.txt";                 MyFileOption myoption = new MyFileOption();                 bool i = myoption.DiskFileOption(sourcepath, targetpath, p);                 if (i == true)                 {                     MessageBox.Show("磁盘文件操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);                 }                 else                 {                     MessageBox.Show("磁盘文件操作时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);                 }             }         }         private void button1_Click(object sender, EventArgs e)         {             richTextBox1.Text = null;             richTextBox1.Focus();         }         /// <summary>         /// 读出文本文件内容         /// </summary>         private void button2_Click(object sender, EventArgs e)         {             MyFileOption myoption = new MyFileOption();             string filepath = @"c:\1.txt";             Int16 i = 0;             string filecontent = "";             myoption.ReadTextFile(filepath, out i, out filecontent);             if (i == 0)             {                 MessageBox.Show(filecontent, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Information);                 richTextBox1.Text = filecontent;             }             else if (i == 1)             {                 richTextBox1.Text = filecontent;                 MessageBox.Show("读取文件成功", "成功", MessageBoxButtons.OK, MessageBoxIcon.Warning);             }             else if (i == 2)             {                 richTextBox1.Text = filecontent;                 MessageBox.Show(filecontent, "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);             }         }         /// <summary>         /// 文件基本属性设置         /// </summary>         private void button5_Click(object sender, EventArgs e)         {             string filepath = @"c:\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);                     }                 }             }         }         /// <summary>         /// 文件夹操作         /// </summary>         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 = @"c:\1";                 string targetpath = @"c:\2";                 MyFileOption myoption = new MyFileOption();                 string[] filesname = null;                 bool i = myoption.DirectoryOption(sourcepath, targetpath, p, out filesname);                 if (i == true)                 {                     MessageBox.Show("磁盘文件夹操作成功", "保存信息", MessageBoxButtons.OK, MessageBoxIcon.Information);                     if (filesname != null)                     {                         foreach (string somestring in filesname)                         {                             richTextBox1.Text += somestring + "\r\n";                         }                     }                 }                 else                 {                     MessageBox.Show("磁盘文件夹操作时出错", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);                 }             }         }     } }

u实验步骤(3):

向项目中添加名为FileOption.cs的类文件,并准备填写关于文件操作的各种方法,如图3-8所示:

图3-8 建立FileOption.cs图

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档