Check if another user have the permission to write a File

倾然丶 夕夏残阳落幕 提交于 2019-12-08 01:32:54

问题


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.



来源:https://stackoverflow.com/questions/4216560/check-if-another-user-have-the-permission-to-write-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!