![](/img/trans.png)
[英]How to create local folder for program data on Windows (in %APPDATA%, without granting permissions, JAVA)
[英]How do I get write permissions in the home folder (or the appdata folder, for windows) in Java?
我試圖在home / appdata文件夾中保存一些用戶設置,但是當我使用dir.canwrite()時它返回false。 這是我用來確定home / appdata文件夾的代碼:
public static String getAppDataPath() {
if (System.getProperty("os.name").contains("Windows")) {
return System.getenv("APPDATA");
} else {
return getUnixHome();
}
}
public static String getUnixHome() {
String home = System.getProperty("user.home");
return home != null ? home : "~";
}
這是嘗試mkdir的代碼:
public static boolean checkExistenceDir(String path) {
File dir = new File(path);
if(!dir.exists()) {
dir.mkdir();
}
}
有問題的路徑是:
getAppDataPath() + ".foo" + File.separatorChar
您需要以管理員身份啟動Java進程。
您可以在Windows中創建一個run.vbs腳本來啟動您的jar :
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "java -jar myjar.jar"
要使程序始終以管理員身份運行,您需要至少以管理員身份運行一次並更新注冊表項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.