检测受密码保护的单词文件 [英] Detect password protected word file

查看:103
本文介绍了检测受密码保护的单词文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用"netoffice"库从Word文件中提取文本.这应该是自动化过程.

I am using "netoffice" library for extracting the text from word files. This should be automated process.

但是,当Word文件受密码保护时,会显示警报窗口,因此用户需要输入密码.由于这是自动过程,因此用户无需输入密码,程序将在此处停止.

However, when the word file is password protected, the alert windows is shown so the user needs to enter the password. Because this is automated process, the user does not enters the password, and the program stops here.

如何检测单词文件是否受到"netoffice"的密码保护,并且如果无法实现,如何禁用警报窗口?

How can I detect if the word file is password protected with "netoffice", and if this is not possible, how can I disable the alert windows from showing up?

我尝试将DisplayAlerts设置为WdAlertLevel.wdAlertsNone,但是它不起作用.

I tried setting DisplayAlerts to WdAlertLevel.wdAlertsNone, but it isn't working.

推荐答案

以下代码段将帮助您跳过受密码保护的文件:

The following piece of code will help you skip password-protected files:

        int iFilesWithPassword = 0;
        Factory.Initialize();
        Application wordApplication = new NetOffice.WordApi.Application();

        try
        {
            // Attempt to open existing document. If document is not password protected then 
            // passwordDocument parameter is simply ignored. If document is password protected
            // then an error is thrown and caught by the catch clause the follows, unless 
            // password is equal to "#$nonsense@!"!                              
            Document newDocument = wordApplication.Documents.Open(@"C:\Users\Giorgos\Desktop\myNextFile.doc",
                                                                  confirmConversions: false,
                                                                  addToRecentFiles: false,
                                                                  readOnly: false,
                                                                  passwordDocument: "#$nonsense@!");



            // read text of document
            string text = newDocument.Content.Text;
        }
        catch(Exception e)
        {
            Exception inner = e.InnerException;

            if (inner != null && inner.InnerException != null)
            {
                inner = inner.InnerException;
                string sErrorMessage = inner.Message;

                if (sErrorMessage.Contains("The password is incorrect."))
                {
                    iFilesWithPassword++;
                }
            }

        }
        finally
        {
            // close word and dispose reference 
            wordApplication.Quit();
            wordApplication.Dispose();
        }

这篇关于检测受密码保护的单词文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆