Mattijs
Mattijs

Reputation: 1930

Creating folder in APPDATA folder on Windows XP (folder is read-only)

In my Java app I find the APPDATA folder and then attempt to create my own subfolder:

    if (System.getProperty("os.name").startsWith("Windows")) { 
        settingsDir = System.getenv("APPDATA") + "\\MyApp\\";

        if (!(new File(settingsDir)).isDirectory()) {
            if (!(new File(settingsDir)).getParentFile().mkdirs()) {
                Error("Failed to create directory " + settingsDir);
            }
        }
    }

On Windows XP this fails, saying the folder could not be created.

The hidden Application Data folder is read-only, and apparently this cannot be changed.

Could this be the reason why creating the new folder fails? If so, what would be the typical way to create a new folder in the APPDATA folder from Java?

Upvotes: 0

Views: 1069

Answers (1)

Mattijs
Mattijs

Reputation: 1930

Ouch, this appears to be a mistake in my own code. I had a left-over getParentFile() in the code. The correct version is:

if (System.getProperty("os.name").startsWith("Windows")) { 
    settingsDir = System.getenv("APPDATA") + "\\MyApp\\";

    if (!(new File(settingsDir)).isDirectory()) {
        if (!(new File(settingsDir)).mkdirs()) {
            Error("Failed to create directory " + settingsDir);
        }
    }
}

I was creating a folder in the parent folder of the APPDATA folder. This was not allowed.

Apparently, although the properties of the APPDATA folder say 'read-only', creating files and folders in this directory is allowed.

My bad, sorry for the noise. I will leave this topic here for the archives.

Upvotes: 1

Related Questions