我正在开发一个示例应用程序,我必须在其中打开一个 excel 文件并检查该文件是否受写保护。代码是
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.
}
}
我想知道文件是否被写保护。