天天看點

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; // [1] 引入檔案操作命名空間

namespace FilesOperate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        // 寫檔案
        private void btnWriteFile_Click(object sender, EventArgs e)
        {
            //[1]建立檔案流
            FileStream fs = new FileStream(@"./huvjie.info", FileMode.Create);// 建立,有則覆寫
            //[2]建立寫入器
            StreamWriter sw = new StreamWriter(fs);
            //[3]以流的方式寫入資料
            sw.Write(this.txtContents.Text.Trim());
            //[4]關閉寫入器
            sw.Close();
            //[5]關閉檔案流
            fs.Close();

            txtContents.Clear();
            System.Diagnostics.Process.Start("notepad", @"./huvjie.info");
        }

        // 讀取檔案
        private void btnReadFile_Click(object sender, EventArgs e)
        {

            try {
                // [1]建立檔案流
                FileStream fs = new FileStream(@"./huvjie.info", FileMode.Open);// 打開的方式
                // [2]建立讀取器
                StreamReader sr = new StreamReader(fs);
                // [3]以流的方式讀取資料
                txtContents.Text = sr.ReadToEnd();
                // [4]關閉讀取器
                sr.Close();
                // [5]關閉檔案流
                fs.Close();
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message, "提示");
            }

        }
        // 增加一個時間,模拟一下日志的記錄,操作步驟和寫入檔案一樣
        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"./log.log", FileMode.Append);// 這裡是追加,無檔案會建立檔案
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("操作成功!" + DateTime.Now.ToString() + "\r\n");

            sw.Close();
            fs.Close();
            //System.Diagnostics.Process[] pr = System.Diagnostics.Process.GetProcessesByName("notepad");
            //if (pr.Length != 0) {
            //    pr[0].Kill();
            //}
            System.Diagnostics.Process.Start("notepad", @"./log.log");
        }
        // 删除檔案
        private void btnDelFile_Click(object sender, EventArgs e)
        {
            File.Delete(@"./log.log");
            File.Delete(@"./huvjie.info");
            File.Delete(@"./log_copy.log");
            File.Delete(@"./huvjie_copy.info");
        }
        // 複制檔案
        private void button2_Click(object sender, EventArgs e)
        {
            // 判斷下要複制過去的路徑下有不有存在的檔案,如果有複制會出現問題,這裡把它删掉了。
            if (File.Exists(@"./log_copy.log") || File.Exists(@"./huvjie_copy.info")) {
                File.Delete(@"./log_copy.log");
                File.Delete(@"./huvjie_copy.info");
            }

            File.Copy(@"./log.log", @"./log_copy.log");
            File.Copy(@"./huvjie.info", @"./huvjie_copy.info");

            System.Diagnostics.Process.Start("notepad", @"./log_copy.log");
            System.Diagnostics.Process.Start("notepad", @"./huvjie_copy.info");
        }
        // 移動檔案和複制檔案差不多 
        private void btnMoveFile_Click(object sender, EventArgs e)
        {
            // 判斷下要複制過去的路徑下有不有存在的檔案,如果有移動會出現問題,這裡把它删掉了。
            if (File.Exists(@"./log_copy.log") || File.Exists(@"./huvjie_copy.info")) {
                File.Delete(@"./log_copy.log");
                File.Delete(@"./huvjie_copy.info");
            }

            File.Move(@"./log.log", @"./0/log_copy.log");
            File.Move(@"./huvjie.info", @"./0/huvjie_copy.info");

            System.Diagnostics.Process.Start("notepad", @"./0/log_copy.log");
            System.Diagnostics.Process.Start("notepad", @"./0/huvjie_copy.info");
        }
        //顯示目前目錄下的檔案
        private void btnGetAllDir_Click(object sender, EventArgs e)
        {
            //string[] files = Directory.GetDirectories(@".");
            string[] files = Directory.GetFiles(@".");
            this.txtContents.Clear();
            foreach (string item in files) {
                txtContents.Text = item + "\r\n";
            }
        }

        // 這個和上面差不多
        private void button2_Click_1(object sender, EventArgs e)
        {
            string[] files = Directory.GetDirectories(@".");
            //string[] files = Directory.GetFiles(@"./");
            this.txtContents.Clear();
            foreach (string item in files) {
                txtContents.Text = item + "\r\n";
            }
        }
        // 建立一個檔案夾
        private void button3_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(@"./檔案夾");
        }

        // 删除指定路徑下所有目錄和檔案
        private void btnDelAllDirOrFiles_Click(object sender, EventArgs e)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(@".\0");
            dirinfo.Delete(true);

            //Directory.Delete(""); // 這個隻能删除為空的目錄
        }
    }
}      

效果: