如何在C#中使用Microsoft Word Library密码保护MS Word .doc或.docx文件? [英] How to password protect a MS Word .doc or .docx file using Microsoft Word Library in C#?

查看:68
本文介绍了如何在C#中使用Microsoft Word Library密码保护MS Word .doc或.docx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用此代码来密码保护MS Word文件。



I have been trying this code to password protect a MS Word file.

public static void PasswordProtectDocument(string fileName, string docPassword)
   {
       Application wordAppwordApp = new Application();
       Document doc = null;
       object missing = System.Reflection.Missing.Value;
       object readOnly = false;
       object visible = true;
       object password = docPassword;
       object fileToOpen = fileName;  //fileName is name of file with its full path
       try
       {
           doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing,
                                           ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                                           ref visible, ref visible, ref missing, ref missing, ref missing);
           doc.Activate();

           doc.SaveAs(ref fileToOpen, ref missing, ref missing, ref missing, ref missing, ref password, ref missing, ref missing,
                      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
       }
       catch (Exception ex)
       {

       }
       finally
       {
           doc.Close(ref missing, ref missing, ref missing);
           wordApp.Quit(ref missing, ref missing, ref missing);
       }
   }





也是这样做但手动打开时,它给出了这种类型的对话框。如果我们输入密码并单击确定,则显示内容。单击取消,它不显示任何内容。但现在的问题是,在单击只读时,它还显示内容在只读模式下。

这会破坏密码保护文件的全部目的。相反,我希望在打开受密码保护的MS Word文件时,对话框不包含任何只读选项。 br />


我猜答案是在另存为或打开功能的参数范围内。请帮我完全保护文件密码。



It is doing also the same but while opening it manually, It is giving this type of dialog box.If we enter the password and click on OK,it is displaying the content.On clicking Cancel,its not displaying anything.But Now the problem is that on clicking Read Only,it is also displaying the content in a read only mode.
This defeats the whole purpose of password protecting a file.So,Instead I want the dialog box not to contain any read only option while opening a password protected MS Word file.

I guess the answer is within the parameters of the Save As or Open function.Kindly help me in completely password protecting a file.

推荐答案

你好朋友,我在这里分享了我用来投影我的Word文件的方法,你可以根据你的要求修改那个方法并解决你的问题。

随着这个方法的帮助,我无法打开文件没有输入密码,即使文件也没有在只读模式下打开。



Hello friend, here i have shared my method which i used to project my Word file, you can modified that method according to your requirement and resolve your issue.
With the help of this method, i can't open a file without entering the password, even the file is not open in read only mode also.

public override string Protect()
      {
          Application app = new Application();
          Document doc = null;

          object missing = System.Reflection.Missing.Value;
          object readOnly = false;
          object visible = false;
          object _Password = Password;
          object fileToOpen = FileName;
          object enforceStyleLock = false;
          try
          {
              app.Visible = false;
              doc = app.Documents.Open(
                                       ref fileToOpen,
                                       ref missing,
                                       ref readOnly,
                                       ref missing,
                                       ref _Password,
                                       ref missing,
                                       ref missing,
                                       ref missing,
                                       ref missing,
                                       ref missing,
                                       ref missing,
                                       ref visible,
                                       ref visible,
                                       ref missing,
                                       ref missing,
                                       ref missing);

              doc.Activate();
              doc.Password = Password;
              doc.Protect(WdProtectionType.wdAllowOnlyReading,
                              ref missing,
                              ref _Password,
                              ref missing,
                              ref enforceStyleLock);
              doc.ReadOnlyRecommended = false;
              doc.SaveAs(ref fileToOpen,
                          ref missing,
                          ref missing,
                          ref _Password,
                          ref missing,
                          ref _Password,
                          ref enforceStyleLock,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing,
                          ref missing);
              doc.Close(ref missing, ref missing, ref missing);
              app.Quit(ref missing, ref missing, ref missing);
          }
          catch (System.Exception ex)
          {
              if (ex.GetType().FullName != "System.Exception")
              {
                  if (((ExternalException)(ex)).ErrorCode == -2146822880)
                  {
                      throw new Exception("Document is password protected");
                  }
                  else
                  {
                      throw ex;
                  }
              }
              else
              {
                  throw ex;
              }
          }
          finally
          {
              if (doc != null)
                  Marshal.ReleaseComObject(doc);
              if (app != null)
                  Marshal.ReleaseComObject(app);
              GC.Collect();
              GC.WaitForPendingFinalizers();
              GC.Collect();
              GC.WaitForPendingFinalizers();
          }

          return FileName;
      }





试图像这样取消保护...



try to unprotect like this...

/// <summary>
        /// Set un-protect Word File
        /// </summary>
        /// <returns></returns>
        public override void UnProtect()
        {
            Application app = new Application();
            Document doc = null;

            object missing = System.Reflection.Missing.Value;
            object readOnly = false;
            object visible = true;
            object _Password = Password;
            object fileToOpen = FileName;

            try
            {
                doc = app.Documents.Open(
                                               ref fileToOpen,
                                               ref missing,
                                               ref readOnly,
                                               ref missing,
                                               ref _Password,
                                               ref missing,
                                               ref missing,
                                               ref _Password,
                                               ref missing,
                                               ref missing,
                                               ref missing,
                                               ref visible,
                                               ref visible,
                                               ref missing,
                                               ref missing,
                                               ref missing);
                doc.Activate();
                //doc.Password = passWord;
                doc.Unprotect(ref _Password);
                doc.SaveAs(ref fileToOpen,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref _Password,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing,
                            ref missing);
                doc.Close(ref missing, ref missing, ref missing);
                Marshal.ReleaseComObject(doc);
                Marshal.ReleaseComObject(app);
            }
            catch (Exception ex)
            {
                //throw ex;
            }
            finally
            {
                if (doc != null)
                    Marshal.ReleaseComObject(doc);
                Marshal.ReleaseComObject(app);
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

        }


这篇关于如何在C#中使用Microsoft Word Library密码保护MS Word .doc或.docx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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