当我们使用 DirectoryInfo dir = Directory.CreateDirectory(pathName) 创建目录或者创建一个文件后,有时作为临时文件用完以后需要删除掉,使用File.delete()或者Directory.Delete()经常会遇到“访问被拒绝的错误”;这时我们需要设置文件或者文件夹的只读属性,再进行删除。

去除文件夹的只读属性:  System.IO.DirectoryInfo DirInfo = new DirectoryInfo(“filepath”);
                       DirInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;

去除文件的只读属性: System.IO.File.SetAttributes("filepath", System.IO.FileAttributes.Normal);

FileAttributes 枚举

提供文件和目录的属性。

此枚举有一个 FlagsAttribute 属性,允许其成员值按位组合。

命名空间:System.IO
程序集:mscorlib(在 mscorlib.dll 中)

语法

[SerializableAttribute] 
[FlagsAttribute] 
[ComVisibleAttribute(true)] 
public enum FileAttributes

  成员名称 说明
  Archive 文件的存档状态。应用程序使用此属性为文件加上备份或移除标记。 
  Compressed 文件已压缩。 
  Device 保留供将来使用。 
  Directory 文件为一个目录。 
  Encrypted 该文件或目录是加密的。对于文件来说,表示文件中的所有数据都是加密的。对于目录来说,表示新创建的文件和目录在默认情况下是加密的。 
  Hidden 文件是隐藏的,因此没有包括在普通的目录列表中。 
  Normal 文件正常,没有设置其他的属性。此属性仅在单独使用时有效。 
  NotContentIndexed 操作系统的内容索引服务不会创建此文件的索引。 
  Offline 文件已脱机。文件数据不能立即供使用。 
  ReadOnly 文件为只读。 
  ReparsePoint 文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。 
  SparseFile 文件为稀疏文件。稀疏文件一般是数据通常为零的大文件。 
  System 文件为系统文件。文件是操作系统的一部分或由操作系统以独占方式使用。 
  Temporary

文件是临时文件。文件系统试图将所有数据保留在内存中以便更快地访问,而不是将数据刷新回大容量存储器中。不再需要临时文件时,应用程序会立即将其删除。

FileAttributes的简单应用

常用的FileAttributes成员有Hidden,System,Archive,ReadOnly,Directory等等。这些对象可以进行位域运算,通过位或运算给文件附上属性。如:File.setAttribute(filename,FileAttribute.Hidden|FileAttribute.ReadOnly)则,filename文件就拥有Hidden和ReadOnly两种属性。

在数值和标志枚举常量之间执行按位“与”操作就可以测试数值中是否已设置标志,这种方法会将数值中与标志不对应的所有位都设置为零,然后测试该操作的结果是否等于该标志枚举常量。
仍然是MSDN中的例子:

using System;
using System.IO;
using System.Text;
class Test
{
  public static void Main()
  {
    string path = @"c:\temp\MyTest.txt";
    // Create the file if it does not exist.
    if (!File.Exists(path))
    {
      File.Create(path);
    }
    if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden)
    {
      // Show the file.
      File.SetAttributes(path, FileAttributes.Archive);
      Console.WriteLine("The {0} file is no longer hidden.", path);
    }
    else
    {
      // Hide the file.
      File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
      Console.WriteLine("The {0} file is now hidden.", path);
    }
  }
}

  

ps:FileAttributes.Archive是文档的存档状态,应用程序使用该属性为文件加上备份或删除标记。

本文摘自:阳子的BLOG

C# 去除文件和文件夹的只读属性的更多相关文章

  1. 探索Windows命令行系列(4):通过命令管理文件和文件夹

    1.文件夹操作 1.1.DIR(directory)命令 1.2.TREE 命令 1.3.CD(change directory)命令 1.4.MD(make directory)命令 1.5.RD( ...

  2. 探索Windows命令行系列(4):通过命令操作文件和文件夹

    1.文件夹操作 1.1.DIR(directory)命令 1.2.TREE 命令 1.3.CD(change directory)命令 1.4.MD(make directory)命令 1.5.RD( ...

  3. C# 删除指定目录下的所有文件及文件夹

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  4. C# 文件和文件夹操作

    一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...

  5. C#文件和文件夹输入输出流代码

    1.建立一个文本文件 public class FileClass { public static void Main() { WriteToFile(); } static void WriteTo ...

  6. python 文件及文件夹操作

    python 文件.目录操作(新增.移动.删除等) python 文件夹与文件操作 mport string, os, sys dir = '/var' print '----------- no s ...

  7. Python学习笔记【第七篇】:文件及文件夹操作

     介绍 我们用pytthon.C#.Java等这些编程语言,想要把文件(文字.视频....)永久保存下来就必须将文件写入到硬盘中,这就需要我们应用程序去操作硬件,我们这些编程语言是无法直接操作硬件的. ...

  8. node 文件、文件夹 增删改查

    1. 文件夹 增加文件夹 var fs = require("fs"); console.log("创建目录 tmp"); fs.mkdir("tmp ...

  9. 利用xcopy在复制文件或文件夹的时候保留其权限

    当用 Windows Explorer 复制或移动文件和文件夹时,文件或文件夹上设置的权限可能会发生改变.例如,当在一个 NTFS文件系统卷内或在两个 NTFS 卷之间复制一个文件时,Windows将 ...

随机推荐

  1. iOS 开发之Target-action模式

    Target-action:目标-动作模式,它贯穿于iOS开发始终.但是对于初学者来说,还是被这种模式搞得一头雾水. 其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那 ...

  2. 编写类String 的构造函数、析构函数和赋值函数

    编写类String 的构造函数.析构函数和赋值函数,已知类String 的原型为:class String{public:String(const char *str = NULL); // 普通构造 ...

  3. python并发获取snmp信息及性能测试

    python & snmp 用python获取snmp信息有多个现成的库可以使用,其中比较常用的是netsnmp和pysnmp两个库.网上有较多的关于两个库的例子. 本文重点在于如何并发的获取 ...

  4. 关于在freemarker模板中遍历数据模型List<JavaBean>的经验

    本文采用简单的servlet作为后台处理数据的工具,前台使用freemarker的ftl模板作为输出工具,简单说明怎样将封装有实体类对象的List集合注入到ftl模板中并且成功的在遍历显示出来,之前在 ...

  5. C#Redis字符串

    上周六通宵打牌周日白天只睡3小时累成狗,从今天起以后不能玩太大的了,小赌怡情大赌伤身,和同事朋友有空玩玩还是好的.今天公司外面马路上有人挂灯笼时死了一个人,哎,快过年了悲剧又发生了,真是生命是脆弱的. ...

  6. angular1.x 脏检测

    写在前面 双向绑定是angular的大亮点,然后支撑它的就是脏检测.一直对脏检测都有一些理解,却没有比较系统的概念. 以下是我阅读网上博文以及angular高级程序设计的理解与总结. 接收指导与批评. ...

  7. Python批量重命名

    某无聊的下午的一个小需求 import os dirPath = r'' #路径 format = r'' #后缀 name = 0 for file in os.listdir(dirPath): ...

  8. html5之datalist标签

    当我看到这个标签的时候,其实我是很愤怒的.因为我以前实现过这个标签的功能,当时是无比的费劲.什么js库呀,function呀.我靠,统统去屎吧,哥有datalist了.那种感觉就好像自己千辛万苦去追去 ...

  9. 本地存储 cookie,session,localstorage( 二)angular-local-storage

    原文:https://github.com/grevory/angular-local-storage#api-documentation Get Started (1)Bower: $ bower ...

  10. Ch2 空间配置器(allocator) ---笔记

    2.1 空间配置器的标准接口 allocator的必要接口: allocator::value_type allocator::pointer allocator::const_pointer all ...