You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Rs.Framework
|
|
{
|
|
public static class DirectoryHelper
|
|
{
|
|
/// <summary>
|
|
/// 移动文件夹的只读属性
|
|
/// </summary>
|
|
/// <param name="dirPath">文件夹路径</param>
|
|
/// <param name="containFile">是否包含文件</param>
|
|
/// <param name="recursive">是否包含子文件夹</param>
|
|
public static void RemoveReadOnlyAttribute(string dirPath,bool containFile=false,bool recursive=false)
|
|
{
|
|
if (!string.IsNullOrEmpty(dirPath))
|
|
{
|
|
DirectoryInfo di = new DirectoryInfo(dirPath);
|
|
if(di!=null)
|
|
{
|
|
di.Attributes = FileAttributes.Normal & FileAttributes.Directory;
|
|
if (containFile)
|
|
{
|
|
//查找所有的文件
|
|
string[] files = Directory.GetFiles(dirPath);
|
|
if (files != null && files.Length > 0)
|
|
{
|
|
foreach (string f in files)
|
|
{
|
|
File.SetAttributes(f, FileAttributes.Normal);
|
|
}
|
|
}
|
|
}
|
|
if (recursive)
|
|
{
|
|
//查找所有的文件夹
|
|
string[] dirs = Directory.GetDirectories(dirPath);
|
|
if (dirs != null && dirs.Length > 0)
|
|
{
|
|
foreach (string d in dirs)
|
|
{
|
|
RemoveReadOnlyAttribute(d);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|