使用Java制作桌面快捷方式和开始菜单项!!

neek2000 2002-06-03 08:08:22
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;
}
}
...全文
178 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sgg1979 2002-06-03
  • 打赏
  • 举报
回复
关注

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧