C#中如何检查excel文件是否被写保护?

发布于 2024-09-01 13:21:45 字数 1126 浏览 8 评论 0原文

我正在开发一个示例应用程序,其中我必须打开一个 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.
    }

}

我想知道该文件是否被写保护。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

就此别过 2024-09-08 13:21:45

您可以像这样获取 FileAttributes:

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

请参阅文档: http ://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx

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

娇纵 2024-09-08 13:21:45

如果您想检查文件是否为只读,则可以使用 File.GetAttributes(),如下所示:

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

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 :)
}
只涨不跌 2024-09-08 13:21:45

我认为您想查看 WorkBook 类的 HasPassword 属性。

更多信息请访问:http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.haspassword%28VS.80%29.aspx

编辑:在下面留下我的旧答案

吗意思是文件或工作簿是否是只读的?

要检查工作簿是否为只读,WorkBook 类具有一个 ReadOnly 属性。

否则,要检查文件,请查看使用框架中的 IO.FileInfo 类来获取文件属性,如以下代码所示:

FileInfo fsi = new FileInfo("filepathandname");
if ((fsi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly )
{
    // it's readonly
}

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
}
给我一枪 2024-09-08 13:21:45

您可以检查 File.GetAttributes

You can check File.GetAttributes

℡寂寞咖啡 2024-09-08 13:21:45

本质上,只读和写保护是同一件事。但是,您可能会遇到无法访问文件的情况,因为该文件正在被另一个进程使用。在这种情况下,您尝试使用 FileShare 打开它,如下所示:

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, 
    FileShare.ReadWrite))
{
    ...
}

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))
{
    ...
}
噩梦成真你也成魔 2024-09-08 13:21:45

您可以检查保护

activeDocument.ProtectionType

You can check protection with

activeDocument.ProtectionType
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文