package Method;
|
import java.io.File;
|
import java.io.IOException;
|
|
/**¸ÃÀàÉèÖóÌÐòΪ¿ª»ú×ÔÆô¶¯*/
|
public class OpenStart {
|
// дÈë¿ì½Ý·½Ê½ÊÇ·ñ×ÔÆô¶¯,¿ì½Ý·½Ê½µÄÃû³Æ,×¢Òâºó׺ÊÇlnk
|
public boolean setAutoStart(boolean yesAutoStart, String lnk) {
|
File f = new File(lnk);
|
String p = f.getAbsolutePath();
|
String startFolder = "";
|
String osName = System.getProperty("os.name");
|
@SuppressWarnings("unused")
|
String str = System.getProperty("user.home");
|
if (osName.equals("Windows 7") || osName.equals("Windows 8") || osName.equals("Windows 10")
|
|| osName.equals("Windows Server 2012 R2") || osName.equals("Windows Server 2014 R2")
|
|| osName.equals("Windows Server 2016")) {
|
startFolder = System.getProperty("user.home")
|
+ "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";
|
}
|
if (osName.endsWith("Windows XP")) {
|
startFolder = System.getProperty("user.home") + "\\¡¸¿ªÊ¼¡¹²Ëµ¥\\³ÌÐò\\Æô¶¯";
|
}
|
if (setRunBySys(yesAutoStart, p, startFolder, lnk)) {
|
return true;
|
}
|
return false;
|
}
|
|
// ÉèÖÃÊÇ·ñËæÏµÍ³Æô¶¯
|
public boolean setRunBySys(boolean b, String path, String path2, String lnk) {
|
File file = new File(path2 + "\\" + lnk);
|
Runtime run = Runtime.getRuntime();
|
File f = new File(lnk);
|
|
// ¸´ÖÆ
|
try {
|
if (b) {
|
// дÈë
|
// ÅжÏÊÇ·ñÒþ²Ø£¬×¢ÒâÓÃϵͳcopy²¼ÖÃΪºÎÒþ²ØÎļþ²»ÉúЧ
|
if (f.isHidden()) {
|
// È¡ÏûÒþ²Ø
|
try {
|
Runtime.getRuntime().exec("attrib -H \"" + path + "\"");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
if (!file.exists()) {
|
run.exec("cmd /c copy " + formatPath(path) + " " + formatPath(path2));
|
}
|
// ÑÓ³Ù0.5Ãë·ÀÖ¹¸´ÖÆÐèҪʱ¼ä
|
Thread.sleep(500);
|
} else {
|
// ɾ³ý
|
if (file.exists()) {
|
if (file.isHidden()) {
|
// È¡ÏûÒþ²Ø
|
try {
|
Runtime.getRuntime().exec("attrib -H \"" + file.getAbsolutePath() + "\"");
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
Thread.sleep(500);
|
}
|
run.exec("cmd /c del " + formatPath(file.getAbsolutePath()));
|
}
|
}
|
return true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
}
|
|
// ½â¾ö·¾¶ÖпոñÎÊÌâ
|
private String formatPath(String path) {
|
if (path == null) {
|
return "";
|
}
|
return path.replaceAll(" ", "\" \"");
|
}
|
|
|
}
|