Check if another user have the permission to write

2019-05-26 22:41发布

问题:

During an installation process I need to check if a userprovided path is writable to a specified user.

The Path (A UNC Path like \fileserver\share) may not be writable to the user, who is executing the Setup.exe (It's a Windows-Only Software), so I think How do you check for permissions to write to a directory or file? may not work for me.

I know in Java7 there will be a new Filesystem-API, but Java7 is not released yet.

EDIT: User 1 - runs the installer User 2 - runs the installed application

If user 1 get an error "Permission denied" I still don't know, about user 2. I need to check the permission for an other useraccount during the process of installation.

I may use net use /user:<user2> <passwd2> but I'm not sure, if I get all information I need. I get the information, that the path exists, but not, if User 2 is allowed to write.

回答1:

before java 7, you don't have standard way to check if a user can write to a java file using standard library. this is because the old java io just is not power enough.

for example, File.canWrite() will not work if consider file owner, etc....

what you can do is check it in directly, for example, try to write a empty line to it. (this need to consider you application,if your application just don't care a blank line at end and auto delete ending blank lines , then it works..... this is just an example, you can get many tricky ways considering your applicaton.