How to Delete a Read-Only File

How do I delete a read-only file?

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);

How to delete read-only files easily?

As mentioned here try to change the permissions for the file using
os.chmod(path_, stat.S_IWRITE) and remove it

How do I delete a directory with read-only files in C#?

Here is an extension method which sets Attributes to Normal recursively, then deletes the items:

public static void DeleteReadOnly(this FileSystemInfo fileSystemInfo)
{
var directoryInfo = fileSystemInfo as DirectoryInfo;
if (directoryInfo != null)
{
foreach (FileSystemInfo childInfo in directoryInfo.GetFileSystemInfos())
{
childInfo.DeleteReadOnly();
}
}

fileSystemInfo.Attributes = FileAttributes.Normal;
fileSystemInfo.Delete();
}

Deleting a read only file using batch file or command

Specify /F, it will force deletion of read-only files. Also see

del /?

However, that will not work when the file is in use.

Read only files cannot be deleted in android using C remove()

You need write permissions to delete a file, as you change it's content deleting it. When you delete a file, you are actually changing a reference count on this file, stored in your file system (e.g. EXT3, EXT4), you are also changing it's deletion time, and a few other informations.
To delete a file, you need to use chmod to grant write access to an user.

How do I delete read-only file in Gradle?

You could remove the readonly flag prior to performing the delete.

task cleanTempDir << {
ant.attrib(readonly: false) {
fileset(dir: 'C:/Temp')
}
delete fileTree('C:/Temp')
}

This will work on Windows only. If you want this to work on Unix you'll want to use Ant's chmod task.

ant.chmod(dir: '/tmp', perm: 'ugo+w')


Related Topics



Leave a reply



Submit