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 The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
mahboub_mo
  • 2,908
  • 6
  • 32
  • 72
  • 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 The GPT Wizard 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,537
  • 2
  • 42
  • 63