c# file类 文件删除、创建、重名、复制设置文件属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConAppFile
{
    class Program
    {  //file类,文件删除、创建、重名、复制设置文件属性。
        static void Main(string[] args)
        {
            string filePath = @"c:\temp\textfile.txt";
            string fileCopyPath = @"c:\temp\textfile_copy.txt";
            string newFileName = @"c:\temp\textfile_newCopy.txt";
            try
            {  //判断文件已经存在
                if (File.Exists(filePath))
                {
                    //删除这个文件
                    File.Delete(filePath);
                }
                //再次创建文件
                FileStream fs = File.Create(filePath);
                fs.Close();
               //复制文件
                File.Copy(filePath, fileCopyPath);
              
                //更改文件名字
                File.Move(fileCopyPath,newFileName);
                //显示创建时间
                Console.WriteLine(File.GetCreationTime(newFileName));

                //....make the file read_only and hidden...
                File.SetAttributes(newFileName,FileAttributes.ReadOnly);
                File.SetAttributes(newFileName, FileAttributes.Hidden);
            }
            catch (IOException ex)
            {

                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }

            Console.ReadLine();
        }
    }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容