首页 > 编程语言 > 详细

FileAttributes Enum

时间:2019-03-29 12:43:00      阅读:148      评论:0      收藏:0      [点我收藏+]

https://docs.microsoft.com/en-us/dotnet/api/system.io.fileattributes?view=netframework-4.7.2

读取FileAttributes

在桌面新建一个文件file-to-delete.txt,设置只读属性。先删除,然后从回收站还原

  [Test]
        public void FileAttributeTest()
        {
            var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var fileName = "file-to-delete.txt";
            var path = Path.Combine(desktop, fileName);
            var attributes = File.GetAttributes(path);
            Console.WriteLine(attributes);
        }

输出结果为 ReadOnly, Archive

 

 

尝试删除一个只读文件

  static void Main(string[] args)
        {
            try
            {
                var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                var fileName = "file-to-delete.txt";
                var path = Path.Combine(desktop, fileName);
                Console.WriteLine();
                if (File.Exists(path))
                {
                    Console.WriteLine(File.GetAttributes(path));
                    File.Delete(path);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.ReadLine();
            }
        }

ReadOnly
System.UnauthorizedAccessException: Access to the path ‘C:\Users\clu\Desktop\file-to-delete.txt‘ is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at ConsoleApp2.Program.Main(String[] args) in C:\Users\clu\source\repos\Edenred\Test\ConsoleApp2\Program.cs:line 19

 

设置FileAttributes

方法1

 File.SetAttributes(path, FileAttributes.Normal);

方法2

 FileInfo info = new FileInfo(path) {Attributes = FileAttributes.Normal};
if (File.Exists(path))
                {
                    Console.WriteLine(File.GetAttributes(path));
 FileInfo info = new FileInfo(path) {Attributes = FileAttributes.Normal};
 Console.WriteLine(File.GetAttributes(path)); File.Delete(path); }

 

FileAttributes Enum

原文:https://www.cnblogs.com/chucklu/p/10620174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!