怎样删除只读文件? [英] How do I delete a read-only file?

查看:152
本文介绍了怎样删除只读文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的折腾下载,一次性项目,电子邮件草稿,这可能是几天有用的,但并不需要永远保存等各种事情垃圾目录。要接管我的机器停止此目录中,我写了一个程序,将删除早于指定天数的所有文件和记录有关删除的文件的数量和它们的大小只是为了好玩一些统计数据。

我注意到了一些项目的文件夹居住方式的时间比他们应该,所以我开始调查。特别是,它似乎在我用了SVN文件夹项目被坚持围绕。事实证明,在使用svn目录的只读文件不被删除。我只是做了一个简单的测试,在一个只读文件,发现 System.IO.File.Delete System.IO.FileInfo.Delete 不会删除只读文件。

我不关心保护文件,这个特殊的目录中;如果一些重要的东西是有它在错误的地方。是否有一个.NET类,可以删除只读文件,还是我将不得不检查只读属性,并带他们?

解决方案

添加一些示例code蒂姆的回答是:

 使用System.IO;

File.SetAttributes(文件路径,FileAttributes.Normal);
File.Delete(文件路径);
 

I've got a junk directory where I toss downloads, one-off projects, email drafts, and other various things that might be useful for a few days but don't need to be saved forever. To stop this directory from taking over my machine, I wrote a program that will delete all files older than a specified number of days and logs some statistics about the number of files deleted and their size just for fun.

I noticed that a few project folders were living way longer than they should, so I started to investigate. In particular, it seemed that folders for projects in which I had used SVN were sticking around. It turns out that the read-only files in the .svn directories are not being deleted. I just did a simple test on a read-only file and discovered that System.IO.File.Delete and System.IO.FileInfo.Delete will not delete a read-only file.

I don't care about protecting files in this particular directory; if something important is in there it's in the wrong place. Is there a .NET class that can delete read-only files, or am I going to have to check for read-only attributes and strip them?

解决方案

Adding some sample code to Tim's answer:

using System.IO;

File.SetAttributes(filePath, FileAttributes.Normal);
File.Delete(filePath);

这篇关于怎样删除只读文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆