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. Eclipse + Pydev问题 : pydev unresolved import

    http://blog.csdn.net/qq_22765745/article/details/71054030http://blog.csdn.net/amghost/article/detail ...

  2. django 1.9.7 css控制模板样式

    问题:css样式不能控制html样式(针对开发环境,不针对生产环境) 现象: django.template.exceptions.TemplateSyntaxError: Invalid block ...

  3. map 的用法

    #include<iostream> #include<map> #include<string> #define s second #define f first ...

  4. form的action属性值对应servlet的web.xml的url-pattern

    <form action="abc">在web.xml里面<servlet><servlet-name>123</servlet-name ...

  5. plsql注册-转

    注册码:Product Code:4t46t6vydkvsxekkvf3fjnpzy5wbuhphqzserial Number:601769 password:xs374ca https://blo ...

  6. npm 包下载的各种姿势

    最近在写Node程序的时候,突然对 npm install 的-save和-save-dev 这两个参数的使用比较混乱.其实博主在这之前对这两个参数的理解也是模糊的,各种查资料和实践后对它们之间的异同 ...

  7. EasyUi通过POI 实现导出xls表格功能

    Spring +EasyUi+Spring Jpa(持久层) EasyUi通过POI 实现导出xls表格功能 EasyUi界面: 点击导出按钮实现数据导入到xls表格中 第一步:修改按钮事件: @Co ...

  8. android TextView Unicde编码转换 android中一些特殊字符Unicode码值

    android TextView Unicde编码转换 android中一些特殊字符Unicode码值 android中一些特殊字符(如:←↑→↓等箭头符号,约等于号≍)的Unicode码值 Text ...

  9. 在CentOS 6.3中安装与配置cmake

    安装说明安装环境:CentOS-6.3安装方式:源码编译安装软件:cmake-2.8.10.2.tar.gz下载地址:http://www.cmake.org/cmake/resources/soft ...

  10. QXDM操作应用

    QXDM(The QUALCOMM Extensible Diagnostic Monitor)是高通公司(Qualcomm)公司发布的可以对手机终端所发数据进行跟踪有效工具,通过对数据的分析可以诊断 ...