gpt4 book ai didi

Java将应用程序 move 到启动文件夹

转载 作者:行者123 更新时间:2023-11-30 08:50:58 24 4
gpt4 key购买 nike

我正在尝试将我的应用程序添加到启动文件夹。

public class info {

public static String getautostart() {
return System.getProperty("java.io.tmpdir").replace("Local\\Temp\\", "Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");
}

public static String gettemp() {
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
return tempDir;
}

public static String getrunningdir() {
String runningdir = ProjectGav.class.getProtectionDomain().getCodeSource().getLocation().getPath();
return runningdir;
}

}

这是我存储信息方法的类

还有主类:

    System.out.println(info.getautostart() + "\\asdf.jar");
System.out.println(info.getrunningdir());
Files.move(info.getrunningdir(), info.getautostart() + "\\asdf.jar");

这是 println 的输出:

C:\Users\JOHNDO~1\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\asdf.jar

/C:/Users/John%20Doe/Downloads/project1.jar

files.move 不工作。

最佳答案

OK 假设您想将 jar 从当前目录 move 到自动启动文件夹。您已经拥有这些方法:

// nothing to change here - it seems to work just fine
public static String getautostart() {
return System.getProperty("java.io.tmpdir").replace("Local\\Temp\\", "Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");
}

// this one got confused with the '/' and '\' so I changed it
public static String getrunningdir() {
//you need to import java.nio.file.Paths for that.
String runningdir = Paths.get(".").toAbsolutePath().normalize().toString(); // it simulates the creation of a file in the current directory and returns the path for that file
return runningdir;
}

现在 move 文件需要 Paths 而不是 String 因此您需要为每个字符串创建一个 Path 实例:

 Path autostartPath = Paths.get(getautostart());
Path currentPath = Paths.get(getrunningdir());

如果你想指向路径中的一个文件(比如你的 .jar 文件),你可以这样做:

currentPath.resolve("myProgram.jar"); // The paths end with a `/` so you don't need to add it here

所有的 move 应该是这样的:

Files.move(currentPath.resolve("myProgram.jar"), autostartPath.resolve("myProgram.jar"));

关于Java将应用程序 move 到启动文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30742310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com