C#中File静态类及其常用静态方法实例详解

来源:互联网 发布:大数据分析挖掘需求 编辑:程序博客网 时间:2024/04/30 12:10

刚才写了一个小的测试程序,全面详述了c#中的关于文件操作的各种静态方法的用法,现分享一下----------YYC

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;


namespace PraccticeInDetail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            File.Copy("E:\\a.txt","F:\\b.txt");
            /*
             * File.Copy(path1, path2);
             * 文件路径中必须包含文件名,且两个文件名可以相同。
             * 把path1的文件复制到path2
             * path1的文件必须存在,否则会报错。
             * path2的文件必须不存在,否则也会保存
             * 
             * */
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (File.Exists("E:\\a.txt"))
            {
                MessageBox.Show("该文件存在!");
            }
            else {
                MessageBox.Show("该文件不存在!");
            }


            /*
             * File.Exists(path1);
             * 文件路径必须包含文件名
             * 如果文件存在则返回true,不存在则返回false
             * 
             */
        }


        private void button3_Click(object sender, EventArgs e)
        {
            File.Replace("E:\\a.txt","E:\\c.txt","E:\\bBack.txt");
            /*File.Replzce(path1,path2,path3);
             * 使用path1文件的内容替换path2文件的内容
             * 该函数会首先删除“path2”文件,并同时为该文件创建path3的备份,最后把path1的写入一个新的path2文件中
             * path1,和path2文件都必须存在
             * 
             */
           File.Replace("E:\\a.txt", "E:\\c.txt", "E:\\bBack.txt",true);
            /*最后一个参数为bool型
             * true表示可以忽略文件的合并错误(主要为文件的“属性”,“控制列表”等和并时可能发生的错误)
             * false表示不可忽略
             */
        }


        private void button4_Click(object sender, EventArgs e)
        {
            File.Delete("E:\\b.txt");
            //删除该路径指定的文件
            //该函数没有返回值
        }


        private void button5_Click(object sender, EventArgs e)
        {   //获取文件的创建时间,返回值为DateTime类型
            DateTime dt = File.GetCreationTime("E:\\a.txt");
            //重新设置文件的创建时间
            File.SetCreationTime("E:\\a.txt",dt.AddDays(10));
            //获得文件的最后一次访问时间
            DateTime dt1 = File.GetLastAccessTime("E:\\a.txt");
            //重新设置文件的最后一次访问时间
            File.SetLastAccessTime("E:\\a.txt",dt1.AddHours(55));
        }


        private void button6_Click(object sender, EventArgs e)
        {   //获得文件的控制属性,返回值为FileAttributes类型的枚举值
            //FileAttributes枚举包括{Nomal(普通文件),Arcive(存档文件),ReadOnly(只读文件),Hidden(隐藏文件),Compressed(压缩文件)}
            FileAttributes fa = File.GetAttributes("E:\\a.txt");
            if ((fa & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)  //用二进制运算经行判断
            {
                MessageBox.Show("该文件为只读文件!");
            }
            if ((fa & FileAttributes.Hidden) == FileAttributes.Hidden)
            {
                MessageBox.Show("隐藏文件!");
            }
        }


        private void button7_Click(object sender, EventArgs e)
        {   /* WriteAllText(path1,str);
             * 写入一个字符串到path1的文件里去,并覆盖该文件里的原有内容
             * 若path1的文件不存在,则自动创建一个新的文件
             */
            File.WriteAllText("E:\\a.txt","File.writeAllText");
            /*ReadAllText(path1);
             *将文件path1里的内容读取做为一个字符串返回,但也会保留换行符。
             */
            string str = File.ReadAllText("E:\\a.txt");
            MessageBox.Show(str);
        }


        private void button8_Click(object sender, EventArgs e)
        {   
            string[] strArray = {"aa","bb","cc"};
            //像该文件中写入一个字符串数组,并覆盖原有内容,其中每个字符串独自占据一行
            File.WriteAllLines("E:\\a.txt",strArray,Encoding.Default);
            //逐行读取文件的内容,并返回一个字符串数组,其中每个字符串占局源文件中的一行
            strArray = File.ReadAllLines("E:\\a.txt", Encoding.Default);
            MessageBox.Show(strArray[0] + strArray[1] + strArray[2]);
        }


        private void button9_Click(object sender, EventArgs e)
        {
            byte[] a = {1,2,3,4,5};
            //写入一个byte数组到该文件中去,但如果直接双击打开该文件则会显示乱码
            File.WriteAllBytes("E:\\a.txt",a);
            //读取该文件并返回一个byte数组
            a = File.ReadAllBytes("E:\\a.txt");
            MessageBox.Show(a[0].ToString() + a[1].ToString() + a[2].ToString() + a[3].ToString() + a[4].ToString());
        }
    }
}

原创粉丝点击