C# 设置文件目录只读属性,取消只读属性
C# 全选
/// <summary>
/// 文件帮助类
/// </summary>
public class FileHelper
{
/// <summary>
/// 设置目录只读、取消只读
/// </summary>
/// <param name="dirPath"></param>
/// <param name="readOnly"></param>
public static void SetDirectoryReadOnly(string dirPath, bool readOnly)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(dirPath);
if (readOnly)
dir.Attributes = System.IO.FileAttributes.ReadOnly & System.IO.FileAttributes.Directory;
else
dir.Attributes = System.IO.FileAttributes.Normal & System.IO.FileAttributes.Directory;
}
/// <summary>
/// 设置文件只读、取消只读
/// </summary>
/// <param name="filePath"></param>
/// <param name="readOnly"></param>
public static void SetFileReadOnly(string filePath, bool readOnly)
{
if (readOnly)
System.IO.File.SetAttributes(filePath, System.IO.FileAttributes.ReadOnly);
else
System.IO.File.SetAttributes(filePath, System.IO.FileAttributes.Normal);
}
/// <summary>
/// 判断文件是否只读
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool IsReadOnly(string filePath)
{
return System.IO.File.GetAttributes(filePath).HasFlag(System.IO.FileAttributes.ReadOnly);
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网