FileAttributes Enum
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的更多相关文章
- NLog输出目标及类型
targets:输出目标节点 target:配置一个输出目标 Type输出类型: Console 输出到控制台 Debugger 输出到VS输出窗口 File 输出 ...
- 转:NLog之:文件类型目标(File target)
转:http://www.cnblogs.com/RitchieChen/archive/2012/07/16/2594308.html 英文原文[http://nlog-project.org/wi ...
- 十七、C# 反射、特性和动态编程
反射.特性和动态编程 1.访问元数据 2.成员调用 3.泛型上的反射 4.自定义特性 5.特性构造器 6.具名参数 7.预定义特性 8.动态编程 特性(attribute)是在一个程序集中插入 ...
- Nlog 配置总结
Writes log messages to one or more files. Since NLog 4.3 the ${basedir} isn't needed anymore for rel ...
- Nlog日志之File
一:简介 NLog是一个简单灵活的.NET日志记录类库.通过使用NLog,我们可以在任何一种.NET语言中输出带有上下文的(contextual information)调试诊断信息,根据喜好配置其表 ...
- FileAttributes枚举
FileAttributes枚举是一个专门用于标记硬盘上的文件属性的枚举,枚举的说明在这里:http://www.cnblogs.com/kissdodog/archive/2013/01/16/28 ...
- Swift enum(枚举)使用范例
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- 枚举:enum
枚举 所谓枚举就是指定好取值范围,所有内容只能从指定范围取得. 例如,想定义一个color类,他只能有RED,GREEN,BLUE三种植. 使用简单类完成颜色固定取值问题. 1,就是说,一个类只能完成 ...
- Asp.Net 将枚举类型(enum)绑定到ListControl(DropDownList)控件
在开发过程中一些状态的表示使用到枚举类型,那么如何将枚举类型直接绑定到ListControl(DropDownList)是本次的主题,废话不多说了,直接代码: 首先看工具类代码: /// <su ...
随机推荐
- 43. Multiply Strings (大数乘法)
DescriptionHintsSubmissionsDiscussSolution Pick One Given two non-negative integers num1 and num2 ...
- 转自大神的KM想法
我第一次理解KM算法看到大神的讲解不胜感激这km挺神奇的接下来就见识一下这个大牛的吧 转自 http://blog.csdn.net/wuxinxiaohuangdou/article/details ...
- antd-design model 数据特点
- Javascript原生之用cssText批量修改样式
一般情况下我们用js设置元素对象的样式会使用这样的形式: var element= document.getElementById(“id”);element.style.width=”20px”;e ...
- 如何干净卸载mysql
一.在控制面板中卸载mysql软件: 二.卸载过后删除C:\Program Files (x86)\MySQL该目录下剩余了所有文件,把mysql文件夹也删了: 三.windows+R运行“reged ...
- Java开发万年历
自己做出来的万年历: 以下代码: public class Test2 { public static void main(String[] args) { Scanner sc = new Sca ...
- RTP/RTCP 和 SRTP/SRTCP协议(转)
源: RTP/RTCP 和 SRTP/SRTCP协议
- right spindle supply short to gnd-- compact version
hardware guy found that the R1004 lead to this error, but this error should not be checked, because ...
- K8S学习笔记之Flannel解读
0x00 概述 我们知道docker官方并没有提供多主机的容器通信方案,单机网络的模式主要有host,container,brige,none.none这种模式,顾名思义就是docker本身不去管理网 ...
- tomcat日志警告WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did not find a matching property.
日志中有警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'debug' to '0' did ...