使用Java制作桌面快捷方式和开始菜单项!!
import java.io.*;
import javax.swing.*;
public class ShortCut{
public static String osName = System.getProperty("os.name");
public static void main(String[] args){
ShortCut sc = new ShortCut();
File installDir = new File("D:\\install\\");
boolean ok = sc.createWindowsShortcuts(installDir, "install.bat", "install", "winamp");
System.out.println(ok);
}
public boolean createWindowsShortcuts(File installDir, String runnable, String folder, String name){
String command = null;
if(osName.indexOf("9") != -1)
command = "command.com /c cscript.exe /nologo ";
else
command = "cmd.exe /c cscript.exe /nologo ";
if(command != null)
try
{
File shortcutMaker = new File(installDir, "makeshortcut.js");
PrintStream out = new PrintStream(new FileOutputStream(shortcutMaker));
out.println("WScript.Echo(\"Creating shortcuts...\");");
out.println("Shell = new ActiveXObject(\"WScript.Shell\");");
out.println("ProgramsPath = Shell.SpecialFolders(\"Programs\");");
out.println("fso = new ActiveXObject(\"Scripting.FileSystemObject\");");
out.println("if (!fso.folderExists(ProgramsPath + \"\\\\" + folder + "\"))");
out.println("\tfso.CreateFolder(ProgramsPath + \"\\\\" + folder + "\");");
out.println("link = Shell.CreateShortcut(ProgramsPath + \"\\\\" + folder + "\\\\" + name + ".lnk\");");
out.println("link.Arguments = \"\";");
out.println("link.Description = \"" + name + "\";");
out.println("link.HotKey = \"\";");
out.println("link.IconLocation = \"" + escaped(installDir.getAbsolutePath()) + "\\\\" + "winamp.ico,0\";");
out.println("link.TargetPath = \"" + escaped(installDir.getAbsolutePath()) + "\\\\" + runnable + "\";");
out.println("link.WindowStyle = 1;");
out.println("link.WorkingDirectory = \"" + escaped(installDir.getAbsolutePath()) + "\";");
out.println("link.Save();");
out.println("DesktopPath = Shell.SpecialFolders(\"Desktop\");");
out.println("link = Shell.CreateShortcut(DesktopPath + \"\\\\" + name + ".lnk\");");
out.println("link.Arguments = \"\";");
out.println("link.Description = \"" + name + "\";");
out.println("link.HotKey = \"\";");
out.println("link.IconLocation = \"" + escaped(installDir.getAbsolutePath()) + "\\\\" + "winamp.ico,0\";");
out.println("link.TargetPath = \"" + escaped(installDir.getAbsolutePath()) + "\\\\" + runnable + "\";");
out.println("link.WindowStyle = 1;");
out.println("link.WorkingDirectory = \"" + escaped(installDir.getAbsolutePath()) + "\";");
out.println("link.Save();");
out.println("WScript.Echo(\"Shortcuts created.\");");
out.close();
Process p = Runtime.getRuntime().exec(command + " makeshortcut.js", null, installDir);
p.waitFor();
int rv = p.exitValue();
/*try
{
shortcutMaker.delete();
}
catch(Exception exception) { }*/
if(rv == 0)
{
JOptionPane.showMessageDialog(null, "A install test program group has been added to your Start menu\n" + "A install test icon has been added to your desktop.");
return true;
}
else
{
return false;
}
}
catch(Exception e)
{
return false;
}
else
return false;
}
public String escaped(String s)
{
String r = "";
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == '\\')
r = r + '\\';
r = r + s.charAt(i);
}
return r;
}
}