如何删除一个只读文件?

时间:2022-09-11 17:53:17

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.

我注意到有几个项目文件夹的使用时间比应该的要长,所以我开始调查。特别是,我使用SVN的项目的文件夹似乎一直存在。事实证明.svn目录中的只读文件没有被删除。我只是在一个只读文件上做了一个简单的测试,并发现了这个系统。删除和System.IO.FileInfo。删除不会删除只读文件。

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?

我不关心在这个特定目录中保护文件;如果有什么重要的东西在那里,它就在错误的地方。是否有一个.NET类可以删除只读文件,或者我必须检查只读属性并删除它们?

5 个解决方案

#1


139  

Adding some sample code to Tim's answer:

在Tim的回答中添加一些示例代码:

using System.IO;

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

#2


46  

According to File.Delete's documentation,, you'll have to strip the read-only attribute. You can set the file's attributes using File.SetAttributes().

根据文件。删除文档,您将不得不删除只读属性。可以使用file . setattributes()设置文件的属性。

#3


16  

The equivalent if you happen to be working with a FileInfo object is:

如果您碰巧使用的是FileInfo对象,那么相应的操作是:

file.IsReadOnly = false;
file.Delete();

#4


2  

Why do you need to check? Just forcibly clear the read-only flag and delete the file.

为什么需要检查?强制清除只读标志并删除文件。

#5


1  

Hm, I think I'd rather put

嗯,我想我宁愿说

>del /F *

into a sheduled task. Maybe wrapped by a batch file for logging statistics.

日程安排的任务。可能被批处理文件包装,用于记录统计信息。

Am I missing something?

我遗漏了什么东西?

#1


139  

Adding some sample code to Tim's answer:

在Tim的回答中添加一些示例代码:

using System.IO;

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

#2


46  

According to File.Delete's documentation,, you'll have to strip the read-only attribute. You can set the file's attributes using File.SetAttributes().

根据文件。删除文档,您将不得不删除只读属性。可以使用file . setattributes()设置文件的属性。

#3


16  

The equivalent if you happen to be working with a FileInfo object is:

如果您碰巧使用的是FileInfo对象,那么相应的操作是:

file.IsReadOnly = false;
file.Delete();

#4


2  

Why do you need to check? Just forcibly clear the read-only flag and delete the file.

为什么需要检查?强制清除只读标志并删除文件。

#5


1  

Hm, I think I'd rather put

嗯,我想我宁愿说

>del /F *

into a sheduled task. Maybe wrapped by a batch file for logging statistics.

日程安排的任务。可能被批处理文件包装,用于记录统计信息。

Am I missing something?

我遗漏了什么东西?