C#文件夹 | 文件操作

知识框架

在这里插入图片描述

1 C#和文件夹、文件操作介绍

该文档对C#和文件夹、文件、文件内容的读取写入进行入门介绍,使用界面按钮实现各个功能的介绍,主要界面如图1所示
在这里插入图片描述图1 主界面
主界面功能包括:

  1. 创建和删除文件夹
  2. 创建和删除.txt文件
  3. 创建和删除.doc文件
  4. 创建和删除.slxs文件
  5. 将指定的文件内容读取出来,将其显示在文本框中
  6. 将文本框的内容写入到指定文件中

2 程序总览

在这里插入图片描述

3 各功能细讲程序

3.1 创建文件夹

/// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            #region 创建文件夹
            string path = @"F:\c sharp learning\文件及文件夹操作";//文件夹路径,即在该path下创建一个文件夹
            DirectoryInfo directory = new DirectoryInfo(path);//实例化
            if (directory.Exists)//判断该路径"F:\c sharp learning\文件及文件夹操作"是否存在
            {
                toolStripStatusLabel2.Text = "文件夹" + path + "存在";
                //如果路径存在,则在该路径下创建一个文件夹
                directory.CreateSubdirectory("code1");
                toolStripStatusLabel3.Text = "子文件夹code1创建完成";
            }
            else
            {
                toolStripStatusLabel2.Text = "文件夹" + path + "不存在";
                toolStripStatusLabel3.Text = "子文件夹code1创建失败";
            }
            #endregion
        }

3.2 删除文件夹

 /// <summary>
        /// 删除文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            #region 删除文件夹
            DirectoryInfo directoryInfo = new DirectoryInfo(@"F:\c sharp learning\文件及文件夹操作\code1");
            if (directoryInfo.Exists)
            {
                directoryInfo.Delete(true);
                toolStripStatusLabel3.Text = "子文件夹code1删除成功";
            }
            else
            {
                toolStripStatusLabel3.Text = "子文件夹code1不存在";
            }
            #endregion
        }

3.3 创建.txt文件

/// <summary>
        /// 创建.txt文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {

            #region 创建.txt文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的txt文件.txt");//指定文件路径和文件名及文件类型
            if (Directory.Exists(@"F:\c sharp learning\文件及文件夹操作\code1"))
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1存在";
                fileInfo.Create().Close();//创建txt文件,创建后并关闭
            }
            else
            {
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.txt文件创建失败";
            }
            #endregion
        }

3.4 删除.txt文件

/// <summary>
        /// 删除.txt文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            #region 删除.txt文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的txt文件.txt");
            if (fileInfo.Exists)
            {
                toolStripStatusLabel2.Text = "文件-创建的txt文件.txt存在";
                fileInfo.Delete();
                toolStripStatusLabel3.Text = "文件-创建的txt文件.txt删除成功";
            }
            else
            {
                toolStripStatusLabel2.Text = "文件-创建的txt文件.txt不存在";
            }
            #endregion
        }

3.5 创建.doc文件

/// <summary>
        /// 创建.doc文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

            #region 创建.doc文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的doc文件.doc");
            if (Directory.Exists(@"F:\c sharp learning\文件及文件夹操作\code1"))
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1存在";
                fileInfo.Create().Close();//创建txt文件,创建后并关闭
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.doc文件创建成功";

            }
            else
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1不存在";
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.doc文件创建失败";
            }
            #endregion
        }

3.6 删除.doc文件

/// <summary>
        /// 删除.doc文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            #region  删除.doc文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的doc文件.doc");
            if (fileInfo.Exists)
            {
                toolStripStatusLabel2.Text = "文件-创建的doc文件.doc存在";
                fileInfo.Delete();
                toolStripStatusLabel3.Text = "文件-创建的doc文件.doc删除成功";
            }
            else
            {
                toolStripStatusLabel2.Text = "文件-创建的doc文件.doc不存在";
                toolStripStatusLabel3.Text = "";
            }
            #endregion
        }

3.7 创建.xlsx文件

/// <summary>
        /// 创建.xlsx文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            #region 创建.xlsx文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的xlsx文件.xlsx");
            if (Directory.Exists(@"F:\c sharp learning\文件及文件夹操作\code1"))
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1存在";
                fileInfo.Create().Close();//创建txt文件,创建后并关闭
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.doc文件创建成功";
            }
            else
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1不存在";
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.xlsx文件创建失败";
            }
            #endregion
        }

3.8 删除.xlsx文件

/// <summary>
        /// 删除.xlsx文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button8_Click(object sender, EventArgs e)
        {
            #region  删除.xlsx文件
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的xlsx文件.xlsx");
            if (fileInfo.Exists)
            {
                toolStripStatusLabel2.Text = "文件-创建的doc文件.xlsx存在";
                fileInfo.Delete();
                toolStripStatusLabel3.Text = "文件-创建的doc文件.xlsx删除成功";
            }
            else
            {
                toolStripStatusLabel2.Text = "文件-创建的doc文件.xlsx不存在";
                toolStripStatusLabel3.Text = "";
            }
            #endregion
        }

3.9 读取文件内容并显示到richtextbox控件中

/// <summary>
        /// 读取文件内容并显示到richtextbox控件中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button9_Click(object sender, EventArgs e)
        {
            #region 读取文件内容,并将其显示到指定的文本控件中
            richTextBox1.Text = "";
            FileInfo fileInfo = new FileInfo("F:\\c sharp learning\\文件及文件夹操作\\code1\\创建的txt文件.txt");
            if (Directory.Exists(@"F:\c sharp learning\文件及文件夹操作\code1"))
            {
                toolStripStatusLabel2.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1存在";
                if (fileInfo.Exists)
                {
                    toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\创建的txt文件.txt存在";
                    #region 开始读取文件内容
                    string duqu_file_path = @"F:\c sharp learning\文件及文件夹操作\code1\创建的txt文件.txt";
                    StreamReader readstream = new StreamReader(duqu_file_path);//将指定路径的文件转成读取流
                    if (readstream.Peek() == -1)//readstream.Peek() == -1判断该读取流中是否有数据,即判断指定路径的文件中是否有数据
                    {
                        toolStripStatusLabel4.Text = "文件中无内容";
                    }
                    else
                    {
                        while (readstream.Peek() != -1)//readstream.Peek() == -1判断该读取流中是否有数据,即判断指定路径的文件中是否有数据
                        {
                            string getdata_form_wenjian = readstream.ReadLine();
                            richTextBox1.Text += getdata_form_wenjian+"\n";
                        }
                        readstream.Close();
                        toolStripStatusLabel4.Text = "文件中内容读取完毕";
                    }
                    #endregion
                }
                else
                {
                    toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\创建的txt文件.txt不存在";
                }
            }
            else
            {
                toolStripStatusLabel3.Text = @"文件夹F:\c sharp learning\文件及文件夹操作\code1\.txt文件创建失败";
            }
            #endregion
        }

3.10 清除文本框的内容

 /// <summary>
        /// 清除文本框的内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

3.11 将richtextbox控件中内容写入到文件中

/// <summary>
        /// 将richtextbox控件中内容写入到文件中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button10_Click(object sender, EventArgs e)
        {
            #region 将内容写入到文件中:可将内容写入.txt文件、.doc文件和.xlsx文件,过程都一样
            //string write_wenjian_path = @"F:\c sharp learning\文件及文件夹操作\code1\待写入的文件.txt";
            //string write_wenjian_path = @"F:\c sharp learning\文件及文件夹操作\code1\创建的doc文件.doc";
            string write_wenjian_path = @"F:\c sharp learning\文件及文件夹操作\code1\创建的xlsx文件.xlsx";

            StreamWriter writerstream = new StreamWriter(write_wenjian_path);//将指定路径的文件转成写入流
            writerstream.WriteLine(richTextBox1.Text);//将内容写入到写入流中,即将内容写入到指定的文件中-该写入是将已有内容覆盖了
            writerstream.Flush();//刷新该流,即刷新该文件
            writerstream.Close();//关闭该流,即关闭与文件的连接
            #endregion
        }

4 结果展示

4.1 文件夹创建结果如图4.1

在这里插入图片描述

4.2 创建文件如图4.2

在这里插入图片描述

4.3 读取.txt文件结果如图4.3

在这里插入图片描述

4.5 将文本控件的内容写入到指定文件结果如图4.5

在这里插入图片描述
图4.5 (a)内容写入到excel中
在这里插入图片描述
图4.5 (b)将内容写入到word中
在这里插入图片描述
图4.5 (c) 将内容写入到txt中

5 具体代码项目文件如下

对应的项目:https://download.csdn.net/download/asd_8_8_8_8_8_8_asd/16638620

6 引用

文档资料: http://c.biancheng.net/view/2915.html

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值