How to check whether a excel file is write protected or not in C#?

Posted by Pavan Navali on Stack Overflow See other posts from Stack Overflow or by Pavan Navali
Published on 2010-05-13T11:29:23Z Indexed on 2010/05/13 11:34 UTC
Read the original article Hit count: 212

Filed under:
|
|

Hi,

I'm developing a sample application in which I have to open an excel file and check whether the file is write protercteed 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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about excel