1

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.
    }

}

ファイルが書き込み保護されているかどうかを知りたいです。

4

6 に答える 6

3

次のように 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を参照してください。

于 2010-05-13T11:36:41.097 に答える
2

ファイルが読み取り専用かどうかを確認したい場合は、次File.GetAttributes()のようにを使用して確認できます。

if(File.GetAttributes(fileNameAndPath) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
  //it's readonly :)
}
于 2010-05-13T11:36:49.733 に答える
1

クラスのHasPasswordプロパティを見たいと思います。WorkBook

詳細: 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
}
于 2010-05-13T11:35:34.010 に答える
0

で保護を確認できます

activeDocument.ProtectionType
于 2014-02-19T14:14:56.770 に答える
0

基本的に、読み取り専用と書き込み保護は同じものです。ただし、別のプロセスで使用されているためにファイルにアクセスできないという状況が発生する場合があります。この場合、次のように FileShare で開こうとします。

using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, 
    FileShare.ReadWrite))
{
    ...
}
于 2010-05-13T12:07:16.873 に答える
0

File.GetAttributesを確認できます

于 2010-05-13T11:38:20.843 に答える