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

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

问题描述

我正在使用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.

如何检测 word 文件是否受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:UsersGiorgosDesktopmyNextFile.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();
        }

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

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