locked
How Can I get property of a folder in c#? RRS feed

  • Question

  • Hi friends, in my applicatin I want to be sure that a Folder is not readonly. because I write something in it. How can I check it? or How can I change that attribute?
    thanks
    Wednesday, May 13, 2009 1:08 PM

Answers

  • FileAttributes us a Flags enum and it's likely that more than one is set. Therefore you should use bit operators to check for just the ReadOnly flag.


    if ((dr.Attributes & FileAttributes.ReadOnly) != 0)
    Mattias, C# MVP
    • Marked as answer by Harry Zhu Tuesday, May 19, 2009 9:10 AM
    Thursday, May 14, 2009 3:16 PM

All replies

  • static bool IsDirectoryReadOnly(string path)
            {
                if (!Directory.Exists(path))
                {
                    DirectoryInfo dr = new DirectoryInfo(path);
                    if (dr.Attributes == FileAttributes.ReadOnly)
                    {
                        return true;
                    }
                    
                }
    
                return false;
            }
            


    Thanks,
    A.m.a.L
    .Net Goodies
    Remember to click "mark as answered" when you get a correct reply to your question
    • Proposed as answer by Ajith R Nair Thursday, May 14, 2009 3:29 PM
    Wednesday, May 13, 2009 1:26 PM
  • FileAttributes us a Flags enum and it's likely that more than one is set. Therefore you should use bit operators to check for just the ReadOnly flag.


    if ((dr.Attributes & FileAttributes.ReadOnly) != 0)
    Mattias, C# MVP
    • Marked as answer by Harry Zhu Tuesday, May 19, 2009 9:10 AM
    Thursday, May 14, 2009 3:16 PM