zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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(" ", "\" \"");
    }
 
 
}