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. 17. Letter Combinations of a Phone Number(bfs)

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  2. html5-嵌入图片

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. uva 1632 Alibaba

    题意: 一个人要从如果干个地方拿货,每个地方的货物是有存在时间的,到了某个时间之后就会消失. 按照位置从左到右给出货物的位置以及生存时间,这个人选择一个最优的位置出发,问拿完货物的最少时间. 思路: ...

  4. C# CheckBox与RadioButton

    通常RadioBox称为单选按钮,CheckBox称为多选按钮,这两个控件都是从ButtonBase类中派生,可以将其视为按钮. 多个checkBox之间的选择是互相独立的,互补影响.多个RadioB ...

  5. git使用,多分支合并代码解决冲突,git删除远程分支,删除远程master默认分支方法

    git使用,多分支合并代码解决冲突,git删除远程分支,删除远程master默认分支方法提交代码流程:1.先提交代码到自己分支上2.切换到devlop拉取代码合并到当前分支3.合并后有变动的推送到自己 ...

  6. Git HEAD detached from XXX (git HEAD 游离) 解决办法

    本文 Git 图片主要来自:图解 Git,非常感谢! 读完本文你将了解: 什么是 HEAD HEAD 游离状态的利与弊 具体解决操作 Thanks 什么是 HEAD Git 中的 HEAD 可以理解为 ...

  7. 文件缓冲区在fork后复制

    场景:父进程trace进程A,当A进程fork子进程B时,让父进程也fork子进程去trace子进程B,用于trace的进程将被trace的进程发生的系统调用号通过fprintf存入各自文件中 问题: ...

  8. 一个Java系统测试

    实验感受: 本次实验最大的感受,就是不要改代码,自己写,代码改起来真的没完没了,不知道会出现什么问题.还有就是一定要清楚自己要怎么去写,流程很重要,一个个功能去实现. 主界面 数据库 主页面代码 &l ...

  9. 动手动脑-Java的方法重载

    例: Using overloaded methods public class MethodOverload {  public static void main(String[] args) {  ...

  10. The Little Prince-12/09

    The Little Prince-12/09 今天中文书评+自述奥! 也许世界上也有五千朵和你一模一样的花,但只有你是我独一无二的玫瑰. ————喵喵喵,多么美妙的一句表白词呀! 时间会缓和所有的悲 ...