问题
I am using the following code to determine if I can write to a specific directory using QFileInfo:
QFileInfo dinfo(dirname);
if (dinfo.exists())
valid = dinfo.isWritable()
Unfortunately, when I pass in the path of the current user's desktop on Vista 64:
C:\Users\USERNAME\Desktop
QFileInfo::isWritable() returns false. However, if I pass it another directory (say C:\Temp) it returns true. I requested the directory permissions from the QFileInfo object which were 5555 (not writable by anyone). This code works as expected on other platforms including Windows XP. Anybody have any ideas as to what might be going on here.
As a point of reference, if I remove the check I can actually save the file to that location without a problem.
回答1:
So, after a bit of digging through the Task Tracker at Qt, I discovered that QFileInfo::isWritable() is only valid for use on files and not directories. By changing the code to ask if I could create the file of interest instead of asking if the directory is writable, I was able to achieve the desired outcome:
QDir dir(dirname);
if (dir.exists())
{
QFileInfo finfo(dir.absoluteFilePath(fname));
valid = finfo.isWritable();
}
Thanks.
回答2:
I'm making a very wild guess here, but have you checked to see if it is a link, shortcut, alias, or other psuedo-folder? It seems possible to me that you are getting the permissions of a hard-coded symlink, which isn't writable, instead of the permissions of the item it is pointing to.
From the isSymLink()
documentation (bold added by me):
On Unix (including Mac OS X), opening a symlink effectively opens the link's target. On Windows, it opens the .lnk file itself.
So I would check the results of isSymLink()
, and if necessary get the real target from symLinkTarget()
(and see the documentation for the last; the target may or may not actually exist).
回答3:
The directory "C:\Users\USERNAME\Desktop" is by default Read Only on Windows Vista. This doesn't mean that you can't write files to the folder. This mean that you can not adjust the directory itself (name change ect.).
来源:https://stackoverflow.com/questions/1089598/incorrect-qfileinfo-permissions-for-user-desktop-on-vista-64