VSTO系列之如何在文档启动中排除不可使用的环境

前提:

  因为vsto为托管项目,所以framework在不安装情况下,vsto是无法运行的

方案背景:

  在客户实际使用过程中,需要在使用手册中提示填报工具所基于的环境,而这样的操作即不方便,也免不了客户经常给你打电话,问“我的工具怎么不能使用,或者我的工具使用生成的时候报错等异常”能不能让文档自动检测它所处于的环境,这样一个思路,可以提高工具的友好性,而且也能减少这样不必要的麻烦;

处理逻辑是这样的,文档在启动的时候,自动检测它所处的填写环境,如果符合填写环境无提示,如不符合会提示客户环境与所要求不符

而vsto与客户环境office、framework、操作系统都有关系

下面只是一些想法,能不能借鉴和用于下一个项目,后面我会亲自实践到项目中,后面再补充

核心代码块:

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Windows.Forms;
  7 using System.Xml.Linq;
  8 using Microsoft.Office.Tools.Word;
  9 using Microsoft.VisualStudio.Tools.Applications.Runtime;
 10 using Office = Microsoft.Office.Core;
 11 using Word = Microsoft.Office.Interop.Word;
 12 using Microsoft.Win32;
 13 
 14 namespace OfficeCheck
 15 {
 16     public partial class ThisDocument
 17     {
 18         private void ThisDocument_Startup(object sender, System.EventArgs e)
 19         {
 20             
 21         }
 22 
 23         private void ThisDocument_Shutdown(object sender, System.EventArgs e)
 24         {
 25 
 26         }
 27 
 28         //获取操作系统  framework版本  获取office版本
 29         public int ExistsRegedit()
 30         {
 31             int ifused = 0;
 32             RegistryKey rk = Registry.LocalMachine;
 33 
 34             //查询Office2003
 35             RegistryKey f03 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\11.0\Excel\InstallRoot\");
 36 
 37             //查询Office2007
 38             RegistryKey f07 = rk.OpenSubKey(@"SOFTWARE\Microsoft\Office\12.0\Excel\InstallRoot\");
 39 
 40             ////查询wps
 41             //RegistryKey wps = rk.OpenSubKey(@"SOFTWARE\Kingsoft\Office\6.0\common\");
 42 
 43             //检查本机是否安装Office2003
 44             String str = "您的使用环境为:操作系统:\n" + getWindowsVersion() + "\n.netFramework版本:" + Environment.Version.ToString() + "\noffice版本为:";
 45             if (f03 != null)
 46             {
 47                 //string file03 = f03.GetValue("Path").ToString();
 48                 //if (File.Exists(file03 + "Excel.exe")) ifused += 1;
 49                 MessageBox.Show(str + "Office2003");
 50                 ifused = 1;
 51             }
 52 
 53             //检查本机是否安装Office2007
 54 
 55             if (f07 != null)
 56             {
 57                 //string file07 = akey.GetValue("Path").ToString();
 58                 //if (File.Exists(file07 + "Excel.exe")) ifused += 2;
 59                 MessageBox.Show(str + "Office2007");
 60                 ifused = 2;
 61             }
 62 
 63             //检查本机是否安装wps
 64             //if (wps != null)
 65             //{
 66             //    //string filewps = akeytwo.GetValue("InstallRoot").ToString();
 67             //    //if (File.Exists(filewps + @"\office6\et.exe")) ifused += 4;
 68             //    ifused = 3; 
 69             //}
 70             return ifused;
 71         }
 72 
 73         #region VSTO 设计器生成的代码
 74 
 75         /// <summary>
 76         /// 设计器支持所需的方法 - 不要
 77         /// 使用代码编辑器修改此方法的内容。
 78         /// </summary>
 79         private void InternalStartup()
 80         {
 81             this.button1.Click += new System.EventHandler(this.button1_Click);
 82             this.Startup += new System.EventHandler(this.ThisDocument_Startup);
 83             this.Shutdown += new System.EventHandler(this.ThisDocument_Shutdown);
 84 
 85         }
 86 
 87         #endregion
 88 
 89         // 单击事件
 90         private void button1_Click(object sender, EventArgs e)
 91         {
 92             ExistsRegedit();
 93         }
 94 
 95         //获取操作系统版本
 96         public string getWindowsVersion(){
 97             string strClient="";
 98             Version ver = System.Environment.OSVersion.Version;
 99             if (ver.Major == 5 && ver.Minor == 1)
100             {
101                 strClient = "Win XP";
102             }
103             else if (ver.Major == 6 && ver.Minor == 0)
104             {
105                 strClient = "Win Vista";
106             }
107             else if (ver.Major == 6 && ver.Minor == 1)
108             {
109                 strClient = "Win 7";
110             }
111             else if (ver.Major == 5 && ver.Minor == 0)
112             {
113                 strClient = "Win 2000";
114             }
115             else
116             {
117                 strClient = "未知";
118             }
119             return   strClient;
120         }
121     }
122 }

执行效果

 

转载于:https://www.cnblogs.com/hanhuibing/articles/4120878.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值