开发者

is it possible to place a myfile.file in C:/Windows/System32/ location at runtime?

I wish to开发者_如何学运维 place a file named as myFile.file in

C:/Windows/System32/ location.

Here is use java code to place my file. while I execute my program it throws "Access Denied:C:/Windows/System32/myFile.file".

why is it happening? Is it possible to place in that location?


That (and many other) system locations are restricted for admin users/elevated applications. Application data should be stored in the user application data files in the user profile (or the common application data).

If you really must write into the system folder, then you will need to ask the user for permission via UAC, either by using ShellExecute() with the runas verb to run another program, or COM elevation (if that's possible in Java)

Update
See Andrew's answer for a method to get the correct path in Java.


Put myFile.file in a sub-directory of user.home.

The sub-directory could be the package name of the class saving the file. E.G. if your main class is our.com.Main, store the file at ${user.home}/our/com/myFile.file. The reason to use a sub-directory is to help prevent another apps. myFile.file from overwriting or interfering with your own version.

To get the location of user.home, see:

System.getProperty("user.home");

The value of user.home here, is:

Name        Value
user.home   C:\Users\Andrew

This technique will work reliably for Mac. & *nix, as well as Windows.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜