views:

70

answers:

5

Hi,

I'm developing a sample application in which I have to open an excel file and check whether the file is write protected or not. The code is

using System.Windows.Forms;
using Microsoft.Office.Core;

private void button1_Click(object sender, EventArgs e)
{
    string fileNameAndPath = @"D:\Sample\Sample1.xls"; 
    // the above excel file is a write protected.

    Microsoft.Office.Interop.Excel.Application a = 
              new  Microsoft.Office.Interop.Excel.Application();
    if (System.IO.File.Exists(fileNameAndPath))
    {
        Microsoft.Office.Interop.Excel.ApplicationClass app = 
              new Microsoft.Office.Interop.Excel.ApplicationClass();

        // create the workbook object by opening  the excel file.
        app.Workbooks.Open(fileNameAndPath,0,false,5,"","",true,
                           Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
                           "\t",false, true, 0,false,true,0);

        Microsoft.Office.Interop.Excel._Workbook w = 
              app.Workbooks.Application.ActiveWorkbook;

        if (w.ReadOnly)
              MessageBox.Show("HI");
        // the above condition is true.
    }

}

I would like know whether the file is write protected or not.

+1  A: 

I think you want to look at the HasPassword property of the WorkBook class.

More info at: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.haspassword%28VS.80%29.aspx

Edit: Left my old answer below

Do you mean if the file or the workbook is readonly?

To check if the workbook is readonly the WorkBook class has a ReadOnly property.

Otherwise, to check the file, look at using the IO.FileInfo class in the framework to get out the file attributes, like in the following code:

FileInfo fsi = new FileInfo("filepathandname");
if ((fsi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly )
{
    // it's readonly
}
ho1
I tried that, my scenario is a little different. In excel, you can add a write protection password. Tools menu->OPtions->Security tab. Under this Security tab, "File sharing setting for this work book", which has label called "Password to modify". If you give the password, here, you can open a file in read only mode, will ask for password if you want to modify it. I want to check whether password is required to modify the file –
Pavan Navali
Just updated my answer as you wrote your comment :)
ho1
Has password will come in to picture when the file is password protected and not write protected.
Pavan Navali
Well, my final suggestion would then be that maybe it could be the `WriteReserved` property? Otherwise I have no idea.
ho1
I tried that also, if (w.WriteReserved) MessageBox.Show("HI"); but the condition is never true....Anyways thank you very much for your time and concern
Pavan Navali
+1  A: 

You can get the FileAttributes an like this:

if ((File.GetAttributes(fileNameAndPath) & FileAttributes.ReadOnly) > 0)
{
    // The file is read-only (i.e. write-protected)
}

See for documentation: http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx

Daniel Renshaw
+1  A: 

If you want to check if the file is readonly, then you can check using File.GetAttributes(), like this:

if(File.GetAttributes(fileNameAndPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
  //it's readonly :)
}
Nick Craver
A: 

You can check File.GetAttributes

Incognito
A: 

Essentially, ReadOnly and Write-protected are the same thing. However, you might be encountering the situation where you are unable to access a file because it is being used by another process. In this case, you try to open it with a FileShare as below:

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, 
    FileShare.ReadWrite))
{
    ...
}
Daniel Dyson
I agree that Read only and Write protected are same, in general terms. But the scenario in my case is alittle different
Pavan Navali
There two scenarios: 1: You can make a file read only in General tab, and security tab in File properties window.( Right click on the file, and select properties) 2: In excel, you can add a write protection password. Tools menu->OPtions->Security tab. Under this Security tab, "File sharing setting for this work book", which has label called "Password to modify". If you give the password, here, you can open a file in read only mode, will ask for password if you want to modify it. I want to check whether password is required to modify the file –
Pavan Navali
Oh, ok. In that case, my answer doesn't apply.
Daniel Dyson