zoukankan      html  css  js  c++  java
  • 网页调用本地exe,C# 自动创建注册列表

    一。实现思路。

      先说一下需求,由于网页打印标签存在格式问题,所以想用本地程序去打印。

      所需功能:

        1.BS调用CS程序,并且可以传参

        2. CS程序可以本地打印报表。

    二。具体实现,目前实现启动EXE时,自动注册当前路径的注册列表,供网页端调用。代码如下

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Microsoft.Win32;
    
    
    namespace PrintPlug
    {
        public partial class Form1 : Form
        {
    
            // 注册列表Key名
            private string DefaultName = "printPlug";
    
            public Form1()
            {
                try
                {
                    string str = this.GetType().Assembly.Location;
                    if (JudgePrintPlug())
                    {
                        DeletePrintPlug();
                    }
                    AddPrintPlug();
    
                    // 启动打印窗体
                    InitializeComponent();
                }
                catch (Exception)
                {
    
                    throw;
                }
    
            }
    
            /// <summary>
            /// 判断打印插件列表
            /// </summary>
            /// <returns></returns>
            private bool JudgePrintPlug()
            {
                RegistryKey key = Registry.ClassesRoot;
                RegistryKey software = key.OpenSubKey(DefaultName);
                key.Close();
                if (software == null)
                {
                    return false;
    
                }
                else
                {
                    return true;
                }
    
            }
    
            /// <summary>
            /// 删除打印插件列表
            /// </summary>
            /// <returns></returns>
            private void DeletePrintPlug()
            {
                RegistryKey key = Registry.ClassesRoot;
                key.DeleteSubKeyTree(DefaultName, true);
                key.Close();
            }
    
            /// <summary>
            /// 添加注册打印插件列表
            /// </summary>
            /// <returns></returns>
            private void AddPrintPlug()
            {
                // 当前EXE的存放地址
                string path = this.GetType().Assembly.Location;
                string path2 = """+ path + "" "%1"";
                RegistryKey key = Registry.ClassesRoot;
                // 创建printPlug节点,写入参数
                CreateSubKey(key, DefaultName, "URL Protocol", "", true);
    
                // 创建printPlug - DefaultIcon节点,写入参数
                RegistryKey software = key.OpenSubKey(DefaultName, true);
                CreateSubKey(software, "DefaultIcon", "", path, true);
    
                // 创建printPlug - shell节点
                CreateSubKey(software, "shell");
    
                // 创建printPlug - shell - open节点 
                RegistryKey shellKey = software.OpenSubKey("shell", true);
                CreateSubKey(shellKey, "open");
    
                // 创建printPlug - shell - open - command节点 ,写入参数
                RegistryKey openKey = shellKey.OpenSubKey("open", true);
                CreateSubKey(openKey, "command", "", path2, true);
                key.Close();
            }
    
            /// <summary>
            /// 创建注册列表子类
            /// </summary>
            private void CreateSubKey(RegistryKey key, string keyName, string writeName = "", object writeValue = null, bool flag = false)
            {
                key.CreateSubKey(keyName);
                if (flag)
                {
                    RegistryKey writeKey = key.OpenSubKey(keyName, true);
                    writeKey.SetValue(writeName, writeValue, RegistryValueKind.String);
                }
            }
        }
    }
    View Code

    三。后续开发。

      由于项目保密,只能贴出自动创建网页调本地exe程序的代码片段。供大家参考

      后续调研方向:

        1。调用时传参。(这个超级简单,所以搜搜就有了)

        2。exe具体实现功能。(这个各不相同。各凭本事把)
        3。制作安装程序。(这样在浏览器访问不到exe时,提供下载链接,进行下载安装包,并进行安装,这样是为了确保安装路径固定。)

  • 相关阅读:
    前端百度地图开发使用总结
    换电脑后如何同步git本地仓库及分支
    vue mint-ui初次使用总结
    git学习入门总结
    深夜,当音乐响起,数据在MySQL中静静的疯狂计算
    且说Tomcat、Servlet、JSP和Spring
    国庆与中秋7天死磕Web的时光
    Android编程初涉,以控制摄像头为例
    谈现阶段如何形成可行的健康生活习惯方案
    说Java网络编程
  • 原文地址:https://www.cnblogs.com/yasoPeng/p/14202371.html
Copyright © 2011-2022 走看看