Java锁定USB

锁定方法:修改注册表锁定USB,用了一个Timer不停的锁,以防被手动修改注册表解锁。
经测试,可能被360等防火墙给拦截。

USBTimer.java

package com.sun.usb;

import java.util.Date;
import java.util.Timer;

public class USBTimer{
 
	public static void main(String[] args){
		USBLock usbLock = new USBLock();

        Timer timer = new Timer();
        
        //Execute task
        timer.schedule(usbLock, new Date(), 1000);
	}
}

USBLock.java

package com.sun.usb;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;

public class USBLock extends TimerTask{

	@Override
	public void run() {
		Map map = new HashMap();  
		map.put("Start", "4");	//注册表的属性名和属性值  3:解除锁定 4:锁定USB
		
		String commandPrefix = "cmd.exe /k reg add ";  
		String key = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR";

		for (String k : map.keySet()) {  
		    try {
				Runtime.getRuntime().exec(commandPrefix + key + " /v " + k + " /t REG_DWORD /d " + map.get(k) + " /f");
				System.out.println("USB Lock");
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}  
		}  
	}
}

你可能感兴趣的:(Java)