How do I delete a read-only file?

后端 未结 5 1893
囚心锁ツ
囚心锁ツ 2020-12-14 05:14

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 f

相关标签:
5条回答
  • 2020-12-14 05:47

    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().

    using System.IO;
    
    File.SetAttributes(filePath, FileAttributes.Normal);
    File.Delete(filePath);
    
    0 讨论(0)
  • 2020-12-14 05:54

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

    file.IsReadOnly = false;
    file.Delete();
    
    0 讨论(0)
  • 2020-12-14 05:55

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

    0 讨论(0)
  • 2020-12-14 05:57

    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?

    0 讨论(0)
  • 2020-12-14 06:11

    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().

    0 讨论(0)
提交回复
热议问题