首先,介紹一下 用註冊表編輯器實現禁用指定軟件的操作:
禁止用戶運行記事本(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子項
- RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\cto");
- cto.create();
- RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\cto");
- cto.createSubkey("51cto");
2.刪除cto子項
- try {
- RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\cto");
- cto.delete();
- }
- catch(RegistryException re) {
- re.printStackTrace();
- }
3.查看RootKey.HKEY_CURRENT_USER/Software/Adobe下的子項
- RegistryKey adobe = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\Adobe");
- if(adobe .hasSubkeys()) {
- Iterator i =adobe .subkeys();
- while(i.hasNext()) {
- RegistryKey x = (RegistryKey)i.next();
- System.out.println(x.toString());
- } // while
- }
運行結果:
4.枚舉某子項的所有值(v.toString是獲得子項的值,值包括名稱,類型,數據。若獲得子項對應的值數據用v.getData().toString())
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\360desktop");
- if(r.hasValues()) {
- Iterator i = r.values();
- while(i.hasNext()) {
- RegistryValue v = (RegistryValue)i.next();
- System.out.println(v.toString());
- } // while
- }
運行結果:
5.讀指定子項的值
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\360desktop");
- if(r.hasValue("appinstall1")) {
- RegistryValue v = r.getValue("appinstall1");
- System.out.println(v.toString());//
- }
運行結果:
6.設置註冊表中項的值
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,
- "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
- RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1);
- r.setValue(v);
下面實現文章上文提到的修改註冊表禁用記事本的操作:
- package test;
- import ca.beq.util.win32.registry.RegistryKey;
- import ca.beq.util.win32.registry.RegistryValue;
- import ca.beq.util.win32.registry.RootKey;
- import ca.beq.util.win32.registry.ValueType;
- public class JregistryTest {
- public static void main(String[] args) {
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,
- "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");
- //創建一個DWORD類型的值,值數據爲1
- RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1);
- //Explorer項中添加值v
- r.setValue(v);
- //在Explorer下創建子項disallowRun
- RegistryKey disallowRun = r.createSubkey("DisallowRun");
- disallowRun.setValue(new RegistryValue("1",ValueType.REG_SZ, "notepad.exe"));
- }
- }
參照了很多貼子,代碼都是自己編的,不夠整潔,但功能實現了。
如果想進一步瞭解的同學,建議下載相應的javadoc看看,api裏的類不多,很容易掌握