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 FileSystemInfo
s, 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 ...
随机推荐
- windows系统快捷键
1.我的键盘:windows键的开启,需要按住FN键+windows键. 2.windows键 + E,表示打开我的电脑. 3.windows键 + R,打开windows的命令行窗口. 4.wind ...
- Maximum-SubsequenceSum
题目 https://pintia.cn/problem-sets/900290821590183936/problems/900291257604861953 给出一段数列,求数列的最大子列和,并输 ...
- 内网渗透之如何玩转Meterpreter?(nc.exe)
十.Meterpreter常用命令 1.基本命令(包含meterpreter和msf终端.ruby接口.目标shell交互的命令) background(进程隐藏至后台) sessions(查看已经成 ...
- MySQL练习题之参考答案
1.创建表结构和数据 /* Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Sou ...
- 标准I/O流
一.标准输入流 标准输入流对象cin,重点掌握的函数 cin.get() //一次只能读取一个字符 cin.get(一个参数) //读一个字符 cin.get(三个参数) //可以读字符串 cin.g ...
- mysql避免脏读
mysql避免脏读 在MySQL的InnoDB中,预设的Tansaction isolation level 为REPEATABLE READ(可重读) 在SELECT 的读取锁定主要分为两种方式 ...
- 使用js实现登录随机验证码的效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 谈谈Groovy闭包
A closure is a function with variables bound to a context or environment in which it executes. 概述 闭包 ...
- redis启动
在redis的安装目录的bin目录下: 1.启动服务(指定配置文件) ./redis-server redis.conf 2.启动客户端 ./redis-cli
- 模拟windows全盘搜索
循环遍历pc上的文件夹,保存到mysql数据库中,搜索时,从数据库取数据.import osimport datetimeimport pymysqlimport threading def link ...