package com.a;
/**
*@Created: 2009-6-24 下午04:31:37
*@author: liu.xq
*@description: 创建执行文件快捷方式
*/
import java.io.*;
public class MakeShortCut
{
public static String osName = System.getProperty("os.name");
public static void main(String[] args)
{
MakeShortCut sc = new MakeShortCut();
File installDir = new File("D://install//");
boolean ok = sc.createWindowsShortcut(installDir, "install.bat",
"install", "方正智汇");
}
/**
*
* @param installDir
* 文件路径
* @param runnable
* 可执行性文件(***.exe)
* @param folder
* 文件夹名称(本级文件夹名称)
* @param name
* 快捷方式名称
* @return
*/
public boolean createWindowsShortcut(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();
if (rv == 0)
{
// JOptionPane.showMessageDialog(null, "创建成功");
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;
}
}