c# - 在 c# 中的网络浏览器中删除 word 文档中的警报

标签 c# browser

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prototype
{
    public partial class Form1 : Form
    {
        object oDocument;
        Microsoft.Office.Interop.Word._Application wordApp;
        Microsoft.Office.Interop.Word._Document doc; 

        public Form1()
        {
            InitializeComponent();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;
                wordApp = webBrowser1.Document;

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*
            if (sender.Equals(button9))
            {
                openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
            }
            else
                openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx, *.xls, *.pptx, *.pdf, *.odt)|*.doc;*.docx;*.xls;*.pptx;*.pdf;*.odt"; ;

            openFileDialog1.FilterIndex = 1;

            */

        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            oDocument = webBrowser1.Document;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.xls, *.xlsx)|*.xls;*.xlsx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.ppt, *.pptx)|*.ppt;*.pptx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.pdf)|*.pdf";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }
    }
}

我的代码使用网络浏览器来显示 word、excel 和 powerpoint。不幸的是,每次它试图打开它时,首先会出现一个警告框,询问我是要保存、打开还是取消。我应该如何删除它?

最佳答案

您遇到了浏览器安全问题。您不能在没有用户输入的情况下打开其中可能包含恶意项目的文档。

另外...您需要重构您的代码。您拥有的代码太多了,它们完全相同。

 private string GetFileFromUser(string aFilter){
  openFileDialog1.Filter = aFilter
  openFileDialog1.FilterIndex = 1;
  System.Windows.Forms.HtmlDocument document;
  openFileDialog1.FileName = "";
  openFileDialog1.ShowDialog();
  return openFileDialog1.FileName;
 }

 fileName = GetFileFromUser("Office Documents (*.xls, *.xlsx)|*.xls;*.xlsx");

关于c# - 在 c# 中的网络浏览器中删除 word 文档中的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784197/

相关文章:

c# - 抽象类,如何避免代码重复?

c# - 从多人游戏中获取数据库结果

c# - [UWP][C#] 覆盖 Appx.cs 中设置的 backpress 导航

javascript - 在不损失精度的情况下,JavaScript 的最大整数值是多少?

html - 使用位置 :relative and top/margin-top? 的 Firefox 4 呈现差异

html - 如何底部对齐 DIV 元素中的两个元素?

c# - 删除大文本文件中的所有重复项

c# - Protobuf 解码任意消息。 Protobuf 消息多态性

c++ - 备用http端口?

c# - 在 C# 中更改 WebBrowser 的 URL