JavaFx 创建快捷方式及设置开机启动

源码
class ShortCutUtils{

companion object{
    /**
     * 创建快捷方式
     *
     * @param lnkFile 快捷文件
     * @param targetFile 源文件
     */
    fun createShortCut(lnkFile: File, targetFile: File) {
        if (!System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS")) {
            println("当前系统不是window系统,无法创建快捷方式!!")
            return
        }

        val targetPath = targetFile.path
        if (!lnkFile.parentFile.exists()) {
            lnkFile.mkdirs()
        }
        //原快捷方式存在,则删除
        if (lnkFile.exists()) {
            lnkFile.delete()
        }

        lnkFile.appendBytes(headFile)
        lnkFile.appendBytes(fileAttributes)
        lnkFile.appendBytes(fixedValueOne)
        lnkFile.appendBytes(targetPath.toCharArray()[0].toString().toByteArray())
        lnkFile.appendBytes(fixedValueTwo)
        lnkFile.appendBytes(targetPath.substring(3).toByteArray(charset("gbk")))
    }

    /**
     * 设置软件开机启动
     *
     * @param targetFile 源文件
     */
    fun setAppStartup(targetFile: File) {
        val lnkFile = File(targetFile.parentFile, "temp.lnk")
        createShortCut(lnkFile, targetFile)
        val startUpFile = File(startup, "${targetFile.nameWithoutExtension}.lnk")
        //复制到启动文件夹,若快捷方式已存在则覆盖原来的
        lnkFile.copyTo(startUpFile, true)
        //删除缓存的快捷方式
        lnkFile.delete()
    }

    /**
     * 设置软件开机启动
     *
     * @param targetFile 源文件路径
     */
    fun setAppStartup(targetFilePath: String) {
        setAppStartup(File(targetFilePath))
    }

    /**
     * 创建快捷方式
     *
     * @param lnkFilePath 快捷方式文件生成路径
     * @param targetFilePath 源文件路径
     */
    fun createShortCut(lnkFilePath: String, targetFilePath: String) {
        createShortCut(File(lnkFilePath),File(targetFilePath))
    }

    /**
     * 取消开机启动
     *
     * @param targetFile
     */
    fun cancelAppStartup(targetFile: File) {
        val startupDir = File(startup)
        val files = startupDir.listFiles { file -> file.nameWithoutExtension==targetFile.nameWithoutExtension }
        if (files.isNotEmpty()) {
            //删除启动文件夹中的快捷方式文件
            files.first().delete()
        }
    }

    fun cancelAppStartup(targetFilePath: String) {
        cancelAppStartup(File(targetFilePath))
    }

    /**
     * 开机启动目录
     */
    val startup =  "${System.getProperty("user.home")}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\"

    /**
     * 桌面目录
     */
    val desktop = FileSystemView.getFileSystemView().homeDirectory.absolutePath + "\\"

    /**
     * 文件头,固定字段
     */
    private val headFile = byteArrayOf(
        0x4c, 0x00, 0x00, 0x00, 0x01, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
        0xc0.toByte(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46
    )

    /**
     * 文件头属性
     */
    private val fileAttributes = byteArrayOf(
        0x93.toByte(), 0x00, 0x08, 0x00,  //可选文件属性
        0x20, 0x00, 0x00, 0x00,  //目标文件属性
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //文件创建时间
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //文件修改时间
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  //文件最后一次访问时间
        0x00, 0x00, 0x00, 0x00,  //文件长度
        0x00, 0x00, 0x00, 0x00,  //自定义图标个数
        0x01, 0x00, 0x00, 0x00,  //打开时窗口状态
        0x00, 0x00, 0x00, 0x00,  //热键
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //未知
    )

    private val fixedValueOne = byteArrayOf(
        0x83.toByte(), 0x00, 0x14, 0x00, 0x1F, 0x50, 0xE0.toByte(), 0x4F, 0xD0.toByte(),
        0x20, 0xEA.toByte(), 0x3A, 0x69, 0x10, 0xA2.toByte(),
        0xD8.toByte(), 0x08, 0x00, 0x2B, 0x30, 0x30, 0x9D.toByte(), 0x19, 0x00, 0x2f
    )

    /**
     * 固定字段2
     */
    private val fixedValueTwo = byteArrayOf(
        0x3A, 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00,
        0x32, 0x00, 0x04, 0x00, 0x00, 0x00, 0x67, 0x50, 0x91.toByte(), 0x3C, 0x20, 0x00
    )


}

}
Java版
创建快捷方式使用
File lnkFile = new File(“D:\project\javafx\lanzou-downloader\out\蓝奏云批量下载器3.2.lnk”);
File targetFile = new File(“D:\project\javafx\lanzou-downloader\out\蓝奏云批量下载器3.2.jar”);

//创建快捷方式(可以传File对象或者是路径)
ShortCutUtil.createShortCut(lnkFile,targetFile);
上述代码是将lnk文件输出在了同级目录,我们到文件夹中查看,可以发现已经生成成功了,点击也是能正常打开

设置某软件开机启动和取消开机启动
File targetFile = new File(“D:\project\javafx\lanzou-downloader\out\蓝奏云批量下载器3.2.jar”);

//设置开机启动(可以是File对象或者是路径)
ShortCutUtil.setAppStartup(targetFile);

//取消开机启动
ShortCutUtil.cancelAppStartup(targetFile);
这里可以看到,生成的快捷方式已经存在于启动文件夹,这样下次开机的时候就会自动启动软件了

源码
package site.starsone;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.nio.channels.FileChannel;

/**

  • @author StarsOne

  • @url http://stars-one.site

  • @date Create in 2021/06/11 21:28
    /
    public class ShortCutUtil { ;
    /
    *

    • 开机启动目录
      /
      public final static String startup=System.getProperty(“user.home”)+
      “\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\”;
      /
      *
    • 桌面目录
      /
      public final static String desktop= FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath()+"\";
      /
      *
    • 文件头,固定字段
      /
      private static byte[] headFile={0x4c,0x00,0x00,0x00,
      0x01, 0x14,0x02,0x00,0x00,0x00,0x00,0x00,
      (byte) 0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x46
      };
      /
      *
    • 文件头属性
      /
      private static byte[] fileAttributes={(byte) 0x93,0x00,0x08,0x00,//可选文件属性
      0x20, 0x00, 0x00, 0x00,//目标文件属性
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//文件创建时间
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//文件修改时间
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//文件最后一次访问时间
      0x00,0x00,0x00,0x00,//文件长度
      0x00,0x00,0x00,0x00,//自定义图标个数
      0x01,0x00,0x00,0x00,//打开时窗口状态
      0x00,0x00,0x00,0x00,//热键
      0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00//未知
      };
      /
      *
    • 固定字段1
      */
      private static byte[] fixedValueOne={
      (byte) 0x83 ,0x00 ,0x14 ,0x00
      ,0x1F ,0x50 ,(byte)0xE0 ,0x4F
      ,(byte)0xD0 ,0x20 ,(byte)0xEA
      ,0x3A ,0x69 ,0x10 ,(byte)0xA2
      ,(byte)0xD8 ,0x08 ,0x00 ,0x2B
      ,0x30,0x30,(byte)0x9D,0x19,0x00,0x2f
      };

    /**

    • 固定字段2
      */
      private static byte[] fixedValueTwo={
      0x3A ,0x5C ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
      ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00
      ,0x00 ,0x54 ,0x00 ,0x32 ,0x00 ,0x04
      ,0x00 ,0x00 ,0x00 ,0x67 ,0x50 ,(byte)0x91 ,0x3C ,0x20 ,0x00
      };

    /**

    • 生成快捷方式
    • @param start 完整的文件路径
    • @param target 完整的快捷方式路径
      */
      private static void start(String start,String target){
      FileOutputStream fos= null;
      try {
      fos = new FileOutputStream(createDirectory(start));
      fos.write(headFile);
      fos.write(fileAttributes);
      fos.write(fixedValueOne);
      fos.write((target.toCharArray()[0]+"").getBytes());
      fos.write(fixedValueTwo);
      fos.write(target.substring(3).getBytes(“gbk”));
      fos.flush();
      } catch (Exception e) {
      e.printStackTrace();
      }finally {
      if(fos!=null) {
      try {
      fos.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
      }

    /**

    • 解决父路径问题
      */
      private static File createDirectory(String file){
      File f=new File(file);
      //获取父路径
      File fileParent = f.getParentFile();
      //如果文件夹不存在
      if (fileParent!=null&&!fileParent.exists()) {
      //创建文件夹
      fileParent.mkdirs();
      }
      //快捷方式已存在,则删除原来存在的文件
      if (f.exists()) {
      f.delete();
      }
      return f;
      }

    /**

    • 复制文件
    • @param source
    • @param dest
    • @throws IOException
      */
      private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
      FileChannel inputChannel = null;
      FileChannel outputChannel = null;
      try {
      inputChannel = new FileInputStream(source).getChannel();
      outputChannel = new FileOutputStream(dest).getChannel();
      outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
      } finally {
      inputChannel.close();
      outputChannel.close();
      }
      }

    /**

    • 创建快捷方式
    • @param lnkFilePath 快捷方式文件路径
    • @param targetFilePath 快捷方式对应源文件的文件路径
      */
      public static void createShortCut(String lnkFilePath,String targetFilePath) {
      if (!System.getProperties().getProperty(“os.name”).toUpperCase().contains(“WINDOWS”)) {
      System.out.println(“当前系统不是window系统,无法创建快捷方式!!”);
      return;
      }
      start(lnkFilePath,targetFilePath);
      }

    /**

    • 生成快捷方式

    • @param lnkFile 快捷方式文件

    • @param targetFile 快捷方式对应源文件
      */
      public static void createShortCut(File lnkFile,File targetFile) {

      if (!System.getProperties().getProperty(“os.name”).toUpperCase().contains(“WINDOWS”)) {
      System.out.println(“当前系统不是window系统,无法创建快捷方式!!”);
      return;
      }
      start(lnkFile.getPath(),targetFile.getPath());
      }

    /**

    • 设置某软件开启启动

    • @param targetFile 源文件

    • @return 是否设置成功
      */
      public static boolean setAppStartup(File targetFile) {

      File lnkFile = new File(targetFile.getParent(),“temp.lnk”);
      createShortCut(lnkFile,targetFile);
      try {
      //获取不带扩展名的文件名
      String name = targetFile.getName();
      int end = name.lastIndexOf(".");
      String extendName = name.substring(0,end);

       //将软件复制到软件想
       copyFileUsingFileChannels(lnkFile, new File(startup,extendName+".lnk"));
       //删除缓存的快捷方式文件
       lnkFile.delete();
       return true;
      

      } catch (IOException e) {
      System.out.println(“移动到startup文件夹失败”);
      return false;
      }
      }

    /**

    • 设置某软件开启启动
    • @param targetFilePath 源文件路径
    • @return 是否设置成功
      */
      public static boolean setAppStartup(String targetFilePath) {
      File targetFile = new File(targetFilePath);
      return setAppStartup(targetFile);
      }

    /**

    • 取消开机启动

    • @param targetFile
      */
      public static void cancelAppStartup(File targetFile) {
      File startupDir = new File(startup);
      String targetFileName = targetFile.getName();
      int endIndex = targetFileName.lastIndexOf(".");
      final String targetName = targetFileName.substring(0, endIndex);

      File[] files = startupDir.listFiles(new FilenameFilter() {
      public boolean accept(File dir, String name) {
      //获取不带扩展名的文件名
      int end = name.lastIndexOf(".");
      String filename = name.substring(0, end);
      if (filename.equals(targetName)) {
      return true;
      }
      return false;
      }
      });
      if (files.length > 0) {
      files[0].delete();
      }
      }

}
USB Microphone https://www.soft-voice.com/
Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值