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

2023-01-31 01:01:09
//=========================第一部分:主界面功能设计=============================

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图

相关文章