java appdata,如何在Java的主文件夹(或Windows的appdata文件夹)中获得写权限?

该博客讨论了在Java中尝试保存用户设置到APPDATA目录时遇到的问题,当使用`dir.canWrite()`检查权限时返回false。作者提供了获取APPDATA路径的代码,并展示了创建目录的尝试。解决方法是启动Java进程时以管理员权限运行,通过修改注册表使程序始终以管理员身份运行。
摘要由CSDN通过智能技术生成

I am trying to save some user settings in the home/appdata folder, but when I use dir.canwrite() it returns false. This is the code I use to determine the home/appdata folder:

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 : "~";

}

And this is the code trying to mkdir:

public static boolean checkExistenceDir(String path) {

File dir = new File(path);

if(!dir.exists()) {

dir.mkdir();

}

}

The path in question is:

getAppDataPath() + ".foo" + File.separatorChar

解决方案

You need the Java process to be started as an Administrator.

Set oShell = CreateObject("Shell.Application")

oShell.ShellExecute "cmd.exe", , , "runas", 1

oShell.Run "java -jar myjar.jar"

To make the program always run as administrator, you need it to run as administrator at least once and update a registry key.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值