java 创建 快捷方式

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;
 }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值