[UWP][C#] when a folder is dragged & dropped files in it aren't deleted

kranthi kumar 206 Reputation points
2024-01-18T08:55:14.5933333+00:00

in my app when i drag & drop a folder i can view details of the files but when i try to delete them i get error that 'file is read-only' , why is that ? await file.DeleteAsync(deletionMode);

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,995 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
814 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pinaki Ghatak 4,535 Reputation points Microsoft Employee
    2024-01-19T15:10:48.5166667+00:00

    Hello Kranthi

    The error message you’re seeing is likely due to the file being marked as read-only. In Universal Windows Platform (UWP) apps, when a file is dragged and dropped from the file explorer into the app, it’s often marked as read-only. This is a security feature to prevent unauthorized modifications to the file.

    However, there are ways to check if a file has a read-only attribute and to modify it. Here’s a brief overview:

    1. Check if a file is read-only: You can use the StorageFile.Attributes property in combination with the FileAttributes.ReadOnly flag to check if a file is read-only.
    2. Modify a read-only file: If you need to modify a read-only file, you can use the PathIO.WriteBytesAsync method. This method can write to the file even if it’s marked as read-only. However, this should be used cautiously as it could potentially be seen as an exploit. In your case, you could make the folder or the files within as remove the read-only when write is finished.

    Remember, these methods will only work if your app has the necessary permissions to access and modify the files. You might need to adjust your app’s capabilities in its manifest file or let the user grant permissions through a file picker. In C#, you can remove the read-only attribute from a file using the FileAttributes property of the FileInfo or DirectoryInfo class. Here’s a simple example of how you can do this:

    FileInfo file = new FileInfo("path_to_your_file");
    if ((file.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        file.Attributes &= ~FileAttributes.ReadOnly;
    }
    

    In this code:

    Please replace "path_to_your_file" with the actual path to your file. Remember, this code will only work if your UWP app has the necessary permissions to modify the file attributes. You might need to adjust your app’s capabilities in its manifest file or let the user grant permissions through a file picker<sup>23</sup>. Please note that these are general suggestions, and the exact implementation might vary based on your specific use case. If you’re still facing issues, I recommend checking the official UWP documentation. I hope this solves your problem and answers your question.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.