views:

216

answers:

3

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.

A: 

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).

Caleb Huitt - cjhuitt
A: 

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.).

TimW
+1  A: 

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.

Joe Corkery

related questions