我是否必须编写自己的方法来取消文件夹及其内容的 readony 设置?
我有一个文件夹,其中包含一些文件夹,这些文件夹中包含一些文件。碰巧这些文件可能是只读的,这对我来说是不可接受的。
问题是我是否必须编写代码以递归地取消所有文件的只读设置?写起来并不难,但是有没有标准的.NET 方法可以做到这一点?
我当前的解决方案:
private static void SetReadOnly( string path, bool readOnly )
{
foreach ( string directory in Directory.GetDirectories( path ) )
{
DirectoryInfo di = new DirectoryInfo( directory );
if ( readOnly )
{
di.Attributes &= FileAttributes.ReadOnly;
}
else
{
di.Attributes ^= FileAttributes.ReadOnly;
}
SetReadOnly( directory, readOnly );
}
foreach ( string file in Directory.GetFiles( path ) )
{
FileInfo fi = new FileInfo( file );
if ( readOnly )
{
fi.Attributes &= FileAttributes.ReadOnly;
}
else
{
fi.Attributes ^= FileAttributes.ReadOnly;
}
}
}
I have a folder with some folders in it and some files in those folders. It so happens, that those file may be readonly which is not acceptable for me.
The questions is do I have to write the code to recursively unset readonly on all files? It's not difficult to write but is there any standard .NET way to do it?
My current solution:
private static void SetReadOnly( string path, bool readOnly )
{
foreach ( string directory in Directory.GetDirectories( path ) )
{
DirectoryInfo di = new DirectoryInfo( directory );
if ( readOnly )
{
di.Attributes &= FileAttributes.ReadOnly;
}
else
{
di.Attributes ^= FileAttributes.ReadOnly;
}
SetReadOnly( directory, readOnly );
}
foreach ( string file in Directory.GetFiles( path ) )
{
FileInfo fi = new FileInfo( file );
if ( readOnly )
{
fi.Attributes &= FileAttributes.ReadOnly;
}
else
{
fi.Attributes ^= FileAttributes.ReadOnly;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,没有内置方法可以实现这一点。您必须自己编写它,并且您现有的代码看起来可以完成这项工作。
No, there's no built-in method for that. You have to write it yourself, and your existing code looks like it would do the job.