星空网 > 软件开发 > ASP.net

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

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

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

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

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

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

下面是部分代码。完整项目源码:http://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();        }      }    }

 




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

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流