A few days ago want to do a software to intercept before the program, find the next online information did not find the right, suddenly see a computer software to see the software, find a way to think is like him, by modifying the file association to initiate interception.
The principle is this, change the. exe default startup mode for our program, that is, you run the program is the first to enter our program, then we can review the upcoming software, whether to allow him to run.
Then the software startup steps will turn into the following: Run PC software > start our Programs > program to determine whether auditing allows startup > Allow to modify file associations as system default startup mode > start the software > re-modify file association as our program > Prompt > End is not allowed.
Of course, I this program is some problems have not been solved, specific please self-improvement, write a mess, do not know to say clearly not clear, or directly download the project source code to see. Run:
Here's some code. Full project Source:Http://files.cnblogs.com/files/tuzhiyuan/%E6%8B%A6%E6%88%AA%E5%99%A8%E8%BD%AF%E4%BB%B6.rar
1, modify the implementation code of the file association (on-line search, usage: Setassociatedfiletype (". exe", "\"%1\ "%*"); This is changed to the system default boot mode, we want to intercept him on the Modify boot mode for our program, That is: Setassociatedfiletype (". exe", "\" "+ Application.executablepath +" \ "+" \ "%1\"); The Application.executablepath is the full path of the current program, detailed usage see http://www.360doc.com/content/12/1013/18/7123232_241262664.shtml)
private void Setassociatedfiletype (String typeName, string app) { string fileType = Gettypekeyname (typeName); C2/>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, to get the file path of the startup program, to judge the authorization, modify the Program.cs file Main method
application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); if 0 ) { new Form1 (args[0]); Application.Run (f); } Else { // normal operation Application.Run (new Form1 ()); }
2.2. Get the Startup program file path, to determine this authorization, in our program's main window code plus
PublicForm1 (stringfileName) {InitializeComponent2 (); stringc = File.readalltext ("D:\\config.txt"); //MessageBox.Show (c); stringFileNames = filename.substring (Filename.lastindexof ("\\") +1);//the last \ after dataFileNames = Filenames.replace (". EXE",""); if(FileNames = ="Interceptor Software") {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 ("an unauthorized program is trying to run and has been rejected! Target file:"+filename, filename); This. Visible =false;//Hide Form This. Hide (); This. Enabled =false; This. Opacity =0; //MessageBox.Show ("unauthorized program attempted to run, has been denied!"). Target file: "+ fileName," Security Warning ", MessageBoxButtons.OK, messageboxicon.warning); //This . Close (); } } }
C # WinForm Software startup interception (implemented by changing file associations)