3

I use Word.Interop to work with Word Document and let user to open a file from hard disk.

Sometimes I get error saying that the file that user has chosen is readonly.

How can I check if a file is readonly or not?

Shadow Wizard Says No More War
  • 64,101
  • 26
  • 136
  • 201
mahboub_mo
  • 2,858
  • 5
  • 29
  • 69
  • 1
    possible duplicate of [How to check whether a excel file is write protected or not in C#?](http://stackoverflow.com/questions/2826352/how-to-check-whether-a-excel-file-is-write-protected-or-not-in-c) – Shadow Wizard Says No More War Aug 12 '12 at 08:31

1 Answers1

7

Are you sure you are actually talking about the File attribute (that can be set via the Windows file properties dialog)? If so, you can use FileInfo.IsReadOnly:

FileInfo fileInfo = new FileInfo(@"path\to\file");
if (fileInfo.IsReadOnly)
{
    // do something
}

otherwise, refer to this answer if another process is using the file.

Community
  • 1
  • 1
Adam
  • 15,104
  • 2
  • 41
  • 63