-1

I have a file on disk. I want to check if its readonly. If yes, I want to make it writeable. Make modifications and save it. And change it back to readonly. To do this I am trying to execute the below code in c#. It removes the readonly attribute and let me write and save modifications. However, it fails to set it back to readonly.

Appreciate your help.


public class Test
{
    public static void Main(string[] args)
    {
        //This is a readonly file
        string path = @"c:\temp\MyTest.txt";
        FileAttributes initialattributes = File.GetAttributes(modelFilename);
        if ((initialattributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            //Removing the readonly attribute                      
            initialattributes = RemoveAttribute(initialattributes, FileAttributes.ReadOnly);                 
            File.SetAttributes(path, initialattributes);                                       

            //Performing some write operation and saving file

            //Trying to set the attribute back to readonly but its not working
            File.SetAttributes(path, File.GetAttributes(modelFilename) | FileAttributes.ReadOnly);
       }

   }

   private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
   {
       return attributes & ~attributesToRemove;
   }

}
user3073180
  • 105
  • 1
  • 13
  • 2
    Possible duplicate of [How to make a "Read only" file?](https://stackoverflow.com/questions/11777924/how-to-make-a-read-only-file) – Samvel Petrosov Sep 04 '18 at 09:11
  • 1
    Should work. Are you definitely closing the file before trying to change its attributes back? – Matthew Watson Sep 04 '18 at 09:14
  • 1
    That "duplicate question" has the same code as the OP is using. This isn't a duplicate question - OP doen't want to know *how* to do it; OP wants to know why the apparently correct approach doesn't work. – Matthew Watson Sep 04 '18 at 09:15

1 Answers1

2

This is only a guess, however your problem is likely because you are setting and getting attributes on different files. I.e path and modelFilename

File.SetAttributes(path, File.GetAttributes(modelFilename) | FileAttributes.ReadOnly);

Why are those names different? Why not try

File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);

Or even just as a test

File.SetAttributes(path,  FileAttributes.ReadOnly);

Though on reading your code, its unclear what you are doing with the modelFilename and initialattributes and why its different to path and why you cant just call

File.SetAttributes(path, initialattributes);

also, yes make sure the file handle is closed as mentioned in the comments, this could be a deal breaker (unknown)

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Thanks for your response. modelFileName is originally used in my code. I replaced it with path while posting on stack overflow. Sorry about the confusion. However, with your suggestion for file handle being closed, I revisited the code and could fix that part and now this code is working. – user3073180 Sep 04 '18 at 09:56