createDirectory in Program Files

I came across a funny problem today. A client wants a plugin to store application in the “C:/Program Files” directory.

I use the command:

const String appDirPath = File::getSpecialLocation(File::globalApplicationDirectory).getFullPathName() + “\MyProgramName”;

I DBG the string to make sure it’s correct:
… it is (correctly) “C:\Program Files\MyProgramName”

I do a check …

which comes back true …
then create the directory …


which also comes back true.

However … the directory is not created.

Not so surprising, since you need administrator privileges to create a directory in “Program Files” … but since hasWriteAccess() came back true, and the createDirectory command came back true … I am stumped as to how to indicate what went wrong.

One other note :::
I tried using the File::separatorString (instead of the “//”) … but for some reason this causes the plugin to be unloadable! Perhaps adding two const Strings in a header is a problem … not sure why that would be … but I tried it several times and every time the plugin would suddenly refuse to load …huh.

First of all, never concatenate file paths using strings! It never ceases to amaze me how often I see people doing that, but it’s just terrible programming technique! Always use getChildFile or getSiblingFile.

const String appDirPath = File::getSpecialLocation(File::globalApplicationDirectory).getChildFile (“MyProgramName”);

…I guess I could make getFullPath() return some kind of intermediate string holder class that privately overloads operator+() , so that code like yours would fail to compile… hmm, that might save me a lot of time repeating myself about this!

Are you 100% sure that createDirectory returned true? I don’t really see how it could do, since the function looks like this:

[code]bool File::createDirectory() const
if (! isDirectory())
const File parentDir (getParentDirectory());

    if (parentDir == *this || ! parentDir.createDirectory())
        return false;

    createDirectoryInternal (fullPath.trimCharactersAtEnd (separatorString));
    return isDirectory();

return true;


…so for createDirectory() to return true incorrectly, isDirectory() would have to be broken, and if that was broken, almost no apps would run correctly!

Maybe you are getting tricked by the “virtual store” of vista ?

1 Like

Yeah, I thought you might come back at me for the strings. I had noticed in another post not to do that, but haven’t weened myself off the habit yet. Will make sure to use the getChildFile() function more.

On this virtual store …holy crap! That’s likely it. CreateDirectory() definitely returns true …as does a check later with existsAsDirectory() … so it’s creating something. I’m not at that computer now, but will check in the virtual store path in a bit and see if I can find the directory in there.

I’m guessing there is no easy way around this … and really, it doesn’t matter much for me, I can work around it. Still, it’s an annoying “feature” of windows if you ask me.

I create all my folders inside “ProgramData” ever since I had problems with “Program Files”. You can try that too.