簡體   English   中英

如何在Java中的主文件夾(或appdata文件夾,用於Windows)中獲得寫入權限?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM