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); }

How do I delete a directory with read-only files in C#?

Simplest way of avoiding recursive calls is by utilising the AllDirectories option when getting FileSystemInfos, like so:

public static void ForceDeleteDirectory(string path)
{
var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal }; foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
info.Attributes = FileAttributes.Normal;
} directory.Delete(true);
}

FileAttributes Enum的更多相关文章

  1. NLog输出目标及类型

    targets:输出目标节点 target:配置一个输出目标 Type输出类型: Console        输出到控制台 Debugger     输出到VS输出窗口 File        输出 ...

  2. 转:NLog之:文件类型目标(File target)

    转:http://www.cnblogs.com/RitchieChen/archive/2012/07/16/2594308.html 英文原文[http://nlog-project.org/wi ...

  3. 十七、C# 反射、特性和动态编程

    反射.特性和动态编程   1.访问元数据 2.成员调用 3.泛型上的反射 4.自定义特性 5.特性构造器 6.具名参数 7.预定义特性 8.动态编程   特性(attribute)是在一个程序集中插入 ...

  4. Nlog 配置总结

    Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...

  5. Nlog日志之File

    一:简介 NLog是一个简单灵活的.NET日志记录类库.通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表 ...

  6. FileAttributes枚举

    FileAttributes枚举是一个专门用于标记硬盘上的文件属性的枚举,枚举的说明在这里:http://www.cnblogs.com/kissdodog/archive/2013/01/16/28 ...

  7. Swift enum(枚举)使用范例

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  8. 枚举:enum

    枚举 所谓枚举就是指定好取值范围,所有内容只能从指定范围取得. 例如,想定义一个color类,他只能有RED,GREEN,BLUE三种植. 使用简单类完成颜色固定取值问题. 1,就是说,一个类只能完成 ...

  9. Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件

    在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码: 首先看工具类代码: /// <su ...

随机推荐

  1. django admim后台不转义提交的html

    autoescape¶ Controls the current auto-escaping behavior. This tag takes either on or off as an argum ...

  2. Java将对象保存到文件中/从文件中读取对象

    1.保存对象到文件中 Java语言只能将实现了Serializable接口的类的对象保存到文件中,利用如下方法即可: public static void writeObjectToFile(Obje ...

  3. 每天记命令:lscpu 和 cat /proc/cpuinfo

    [1]lscpu lscpu命令,查看cpu相关的统计信息. socket 就是主板上插cpu的槽的数目,也就是可以插入的物理CPU的个数(比如上例,可以插入1个CPU). core 就是我们平时说的 ...

  4. BIOS 搭配 MBR/GPT 的开机流程

    鸟哥私房菜书上内容: BIOS 搭配 MBR/GPT 的开机流程 在计算机概论里面我们有谈到那个可爱的BIOS与CMOS两个东西, CMOS是记录各项硬件参数且嵌入在主板上面的储存器,BIOS则是一个 ...

  5. Hive和sparksql中的dayofweek

    dayofweek在hive2.2.0开始支持 ,低版本的hive没有提供原生的dayofweek函数,有时需要用到的时候不甚方便.其实低版本的sparksql和hive中可用以下方式实现dayofw ...

  6. Symfony2 UserSecurityEncoder实现自己的验证方式

    fosuserbundle默认使用sha512加密 如果要实现自己的加密方式 需要继承Symfony\Component\Security\Core\Encoder\BasePasswordEncod ...

  7. mycat工作原理

    Mycat的原理并不复杂,复杂的是代码,如果代码也不复杂,那么早就成为一个传说了. Mycat的原理中最重要的一个动词是“拦截”,它拦截了用户发送过来的SQL语句,首先对SQL语句做了一些特定的分析: ...

  8. php_study progress(1)

    PHP是一种语法简单.功能强大的网络编程语言.在语法格式上,PHP借鉴了广泛流行的C.Java和Perl等编程语言的特点,非常类似于C语言,但比C语言更简单,易学和易用,因此特别适合于学习过C语言,有 ...

  9. SpringMVC中参数接收

    /** *  * SpringMVC中参数接收 * 1.接收简单类型 int String * 2.可以使用对象pojo接收 * 3.可以使用集合数据接收参数 * 页面: name="ids ...

  10. JS笔记—02

    1.String截取:substr:截几位, substring:截到哪. 2.String的操作,例如变大写,小写,本身不会变,只是在栈里交换引用似的 var str = "hello w ...