• c# WinForm软件启动拦截(通过更改文件关联实现)


    前几天想做一个软件启动之前拦截的程序,找了下网上的资料没有找到合适的,突然看到电脑软件某看图软件,找到个思路就是跟他一样的,通过修改文件关联进行启动拦截。

    原理是这样的,更改.exe默认的启动方式为我们的程序,也就是你运行程序是先进入我们的程序的,然后我们就可以对这个即将启动的软件进行审核,是否允许他运行。

    然后软件的启动步骤会变成如下:运行电脑软件>启动我们的程序>程序判断审核是否允许启动>允许的话修改文件关联为系统默认的启动方式>启动该软件>重新修改文件关联为我们的程序>不允许则进行提示>结束。

    当然,我这个程序是有些问题没解决的,具体请自行完善了,写得乱七八糟,不知道说得清不清楚,还是直接下载项目源码看就知道了。运行截图:

    下面是部分代码。完整项目源码:https://files.cnblogs.com/files/tuzhiyuan/%E6%8B%A6%E6%88%AA%E5%99%A8%E8%BD%AF%E4%BB%B6.rar

    1、修改文件关联的实现代码(网上找的,用法: setAssociatedFileType(".exe", "\"%1\" %*");这是改成系统默认的启动方式,我们要拦截他就修改启动方式为我们的程序,即:setAssociatedFileType(".exe", "\"" + Application.ExecutablePath + "\"" + " \"%1\"");其中的application.executablepath是获取当前程序的完整路径,详细用法看http://www.360doc.com/content/12/1013/18/7123232_241262664.shtml)

     private void setAssociatedFileType(string typeName, string app)
            {
    
                string fileType = getTypeKeyName(typeName);
    
                Registry.ClassesRoot.OpenSubKey(fileType + "\\shell\\open\\command", true).SetValue(null, app);
    
            }
    
            private string getAssociatedFileType(string typeName)
            {
    
                string fileType = getTypeKeyName(typeName);
    
                return (string)Registry.ClassesRoot.OpenSubKey(fileType + "\\shell\\open\\command").GetValue(null);
    
            }
        
            private string getTypeKeyName(string typeName)
            {
    
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(typeName);
    
                return (string)key.GetValue(null);
    
            }
    

      2.1、获取启动程序的文件路径,对此进行判断授权,修改Program.cs文件Main方法

    Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (args.Length != 0)
                {
                    Form1 f = new Form1(args[0]);
                  
                    Application.Run(f);
                }
                else
                {
                    //正常运行
                    Application.Run(new Form1());
                }

     2.2、获取启动程序文件路径,对此进行判断授权,在我们程序的主窗口代码加上

     public Form1(string fileName)
            {
                InitializeComponent2();
              
                
                string c = File.ReadAllText("d:\\config.txt");
                //MessageBox.Show(c);
                string FileNames = fileName.Substring(fileName.LastIndexOf("\\") + 1);//最后一个\后的数据
                FileNames = FileNames.Replace(".exe", "");
                if (FileNames == "拦截器软件")
                {
                    setAssociatedFileType(".exe", "\"%1\" %*");
                    System.Diagnostics.Process.Start(fileName);
                    setAssociatedFileType(".exe", "\"" + Application.ExecutablePath + "\"" + " \"%1\"");
                    this.Close();
                }
                else
                {
                    if (c.ToLower().IndexOf(FileNames.ToLower()) != -1)
                    {
    
                        setAssociatedFileType(".exe", "\"%1\" %*");
                        System.Diagnostics.Process.Start(fileName);
                        setAssociatedFileType(".exe", "\"" + Application.ExecutablePath + "\"" + " \"%1\"");
                        this.Close();
                    }
                    else
                    {
                        LanjieTip("未授权程序试图运行,已被拒绝!目标文件:" + fileName, fileName);
                        this.Visible = false; //隐藏窗体
                        this.Hide();
                        this.Enabled = false;
                        this.Opacity = 0;
                       // MessageBox.Show("未授权程序试图运行,已被拒绝!目标文件:" + fileName, "安全警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    
                        //this.Close();
                    }
                }
    
    
            }
  • 相关阅读:
    【机器学习】Softmax 和Logistic Regression回归Sigmod
    【LDA】线性判别式分析
    【MLE】最大似然估计Maximum Likelihood Estimation
    n阶方阵A可逆充分必要条件
    【机器学习】K-Means算法
    【X-Forwarded-For】WEB修改访客IP
    【Ubuntu】ubuntu系统下python3和python2环境自由切换
    【Python】打印object对象
    【linux】dpkg info修复及dpkg: warning: files list file for package
    【php】https请求
  • 原文地址:https://www.cnblogs.com/tuzhiyuan/p/4495316.html
Copyright © 2020-2023  润新知