编程语言
首页 > 编程语言> > c# – 从网页上的按钮打开Word应用程序

c# – 从网页上的按钮打开Word应用程序

作者:互联网

我正在开发一个概念证明Web应用程序:一个带有按钮的网页,用于打开安装在用户PC上的Word应用程序.

我在Visual Studio 2008 Express(Windows XP客户端,LAMP服务器)中遇到了一个C#项目.我已经按照Writing an ActiveX Control in .NET教程进行了一些调整后工作正常.然后我添加了我打开Word的按钮.

问题是我可以从项目中引用Microsoft.Office.Interop.Word,但我无法从网页访问它.错误说“该程序集不允许部分信任的调用者”.

我已经阅读了很多关于.NET安全性的内容,但我现在完全迷失了.免责声明:我从4天前开始使用.NET.

我试图解决这个问题,但我看不到光!
我甚至不知道它是否有可能!

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

using Word = Microsoft.Office.Interop.Word;

using System.IO;
using System.Security.Permissions;

using System.Security;
[assembly: AllowPartiallyTrustedCallers]

namespace OfficeAutomation
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void openWord_Click(object sender, EventArgs e)
        {
            try
            {
                Word.Application Word_App = null;
                Word_App = new Word.Application();
                Word_App.Visible = true;
            }
            catch (Exception exc)
            {
                MessageBox.Show("Can't open Word application (" + exc.ToString() + ")");
            }
        }
    }
}

解决方法:

使用.Net Framework 4 XBAP可以轻松实现:您可以使用WPF XBAP而不是ActiveX.

在项目设置窗口中执行:
签名:unckeck所有盒子. (这个项目不需要签名),
在安全选项卡下,只需将其更改为完全信任.

如果用户希望允许应用程序运行,则会提示用户一次.

标签:c,ms-word,internet-explorer,office-interop,activex
来源: https://codeday.me/bug/20190705/1384627.html