首先,介绍一下 用注册表编辑器实现禁用指定软件的操作:

禁止用户运行记事本(notepad.exe)和计算器(cal.Exe):

 

首先在注册表项HKEY_CURRENT_USER\Software\Microsoft \Windows\CurrentVersion\Policies\Explorer中,新建一个双字节值项DisallowRun,修改其值为1,以允许我们定义禁止允许的程序,然后新建一个注册表项HKEY_CURRENT_USER\Software\Microsoft\ Windows\Current Version\Policies\Explorer\DisallowRun,在其下新建两个字符串值项。第一个值项的名称为1,值为notepad.exe,第二个值项为2,值为calc.exe。如果想禁止更多的程序,可以依次建立名称为3、4等顺序往下排列的值项。修改注册表后立即生效。这时想通过"开始"菜单运行记事本和计算器程序,系统会提示不能进行此操作。

  注意:用户在Windows NT/2000/XP的命令解释器(CMD.exe)窗口中,仍然可以通过输入"notepad.exe"运行记事本。这是因为DisallowRun禁止的只是通过资源管理器Explorer运行的程序,记事本不是通过Explorer启动的,所以就无法禁止了。如果不希望用户可以通过命令解释器运行程序,应该在DisallowRun中将命令解释器(CMD.exe)禁止。另外,此方式还有一个不安全之处,就是如果用户将记事本程序"notepad.exe"更改名称,如改成"note.exe",用户就可以运行它了。

二. 用jRegistry 来操作注册表

 jRegistry它是用JNI来封装WINDOWS注册表API,方便了java开发者访问windows注册表。首先介绍一下jRegistryKey.jar和jRegistryKey.dll,这两个文件是使用jRegistry来操作注册表所必需的文件:一个是jar包,是一个包括了java类的文件;一个是动态链接库文件jRegistryKey.dll,提供了访问注册表所需的本地代码(即C/C++)。

1.将jRegistryKey.jar导入eclipse工程中。(这个就不详细介绍了,不会的google去)

2.将jRegistryKey.dll文件copy到D:\WINDOWS\system32下(我的操作系统装在D盘)

代码分析:

1.在RootKey.HKEY_CURRENT_USER\Software下创建cto子项

 
  
  1. RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\cto");    
  2. cto.create();  

 
  
  1. RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\cto");    
  2. cto.createSubkey("51cto");   

2.删除cto子项

 
  
  1. try { 
  2.    RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\cto"); 
  3.    cto.delete(); 
  4. }  
  5. catch(RegistryException re) { 
  6.    re.printStackTrace(); 

3.查看RootKey.HKEY_CURRENT_USER/Software/Adobe下的子项

 
  
  1. RegistryKey adobe = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\Adobe");    
  2. if(adobe .hasSubkeys()) {    
  3.    Iterator i =adobe .subkeys();    
  4.    while(i.hasNext()) {    
  5.       RegistryKey x = (RegistryKey)i.next();    
  6.       System.out.println(x.toString());    
  7.    } // while    

运行结果:

4.枚举某子项的所有值(v.toString是获得子项的值,值包括名称,类型,数据。若获得子项对应的值数据用v.getData().toString())

 
  
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\360desktop");    
  2. if(r.hasValues()) {    
  3.    Iterator i = r.values();    
  4.    while(i.hasNext()) {    
  5.       RegistryValue v = (RegistryValue)i.next();    
  6.       System.out.println(v.toString());    
  7.    } // while    

 

 

运行结果:

5.读指定子项的值

 
  
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\360desktop");    
  2. if(r.hasValue("appinstall1")) {    
  3.    RegistryValue v = r.getValue("appinstall1");    
  4.    System.out.println(v.toString());//    
  5. }  

运行结果:

6.设置注册表中项的值

 
  
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  
  2.         "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");  
  3.  
  4. RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1); 
  5.  
  6. r.setValue(v); 

下面实现文章上文提到的修改注册表禁用记事本的操作:

 

 
  
  1. package test; 
  2. import ca.beq.util.win32.registry.RegistryKey; 
  3. import ca.beq.util.win32.registry.RegistryValue; 
  4. import ca.beq.util.win32.registry.RootKey; 
  5. import ca.beq.util.win32.registry.ValueType; 
  6.  
  7. public class JregistryTest { 
  8.  
  9.     public static void main(String[] args) { 
  10.         RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  
  11.                 "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");  
  12.         //创建一个DWORD类型的值,值数据为1 
  13.         RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1); 
  14.         //Explorer项中添加值v 
  15.         r.setValue(v); 
  16.         //在Explorer下创建子项disallowRun 
  17.         RegistryKey disallowRun = r.createSubkey("DisallowRun"); 
  18.          
  19.         disallowRun.setValue(new RegistryValue("1",ValueType.REG_SZ, "notepad.exe")); 
  20.     } 

参照了很多贴子,代码都是自己编的,不够整洁,但功能实现了。

如果想进一步了解的同学,建议下载相应的javadoc看看,api里的类不多,很容易掌握