增加文件夹的只读属性的移除功能

master
lhiven 6 months ago
parent fb74fe3107
commit 00287ae481

@ -0,0 +1,54 @@
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);
}
}
}
}
}
}
}
}

@ -58,6 +58,7 @@
<Compile Include="ArrayExt.cs" /> <Compile Include="ArrayExt.cs" />
<Compile Include="ControlManager.cs" /> <Compile Include="ControlManager.cs" />
<Compile Include="CopyObject.cs" /> <Compile Include="CopyObject.cs" />
<Compile Include="DirectoryHelper.cs" />
<Compile Include="EntityHelper.cs" /> <Compile Include="EntityHelper.cs" />
<Compile Include="GuidHelper.cs" /> <Compile Include="GuidHelper.cs" />
<Compile Include="HotKeyManager.cs" /> <Compile Include="HotKeyManager.cs" />

Loading…
Cancel
Save