James
James

Reputation: 2610

create folder in windwos8 access denied

I use the code below to create folder in app installed folder, but I alwasy receive access denied exception.

StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        if (!await CheckIfFolderExist(appDataFolderName))
        {                
            StorageFolder appDataFolder = await appFolder.CreateFolderAsync(appDataFolderName);
            StorageFolder userFolder = await appDataFolder.CreateFolderAsync(userFolderName);
            StorageFolder contactFolder = await appDataFolder.CreateFolderAsync(contactFolderName);
        }
        else
        {
            StorageFolder appDataFolder = await appFolder.GetFolderAsync(appDataFolderName);
            if (!await CheckIfSubFolderExis(appDataFolderName, userFolderName))
            {
                await appDataFolder.CreateFolderAsync(userFolderName);
            }
            if (!await CheckIfSubFolderExis(appDataFolderName, contactFolderName))
            {
                await appDataFolder.CreateFolderAsync(contactFolderName);
            }
        }

 // Check if the app folder exists
    private async Task<bool> CheckIfFolderExist(string folderName )
    {
        bool folderExist = false;
        try
        {
            StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder appDataFolder = await appFolder.GetFolderAsync(folderName);                
            folderExist = true;
            return folderExist;
        }
        catch (FileNotFoundException ex)
        {
            return folderExist;
        }
    }

    // Check if the app subfolder exists
    private async Task<bool> CheckIfSubFolderExis(string folderName,string subFolderName)
    {
        bool subFolderExist = false;
        try
        {
            StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder subFolder = await appFolder.GetFolderAsync(subFolderName);
            subFolderExist = true;
            return subFolderExist;
        }
        catch (FileNotFoundException ex)
        {
            return subFolderExist;
        }
    }

Anyone has ideas?

Upvotes: 1

Views: 1087

Answers (1)

Ross Dargan
Ross Dargan

Reputation: 6021

You can't create a folder in your app data folder. You must use either the LocalFolder, RomaingFolder or TemporaryFolder (all found in the ApplicationData class)

The reason for this is to support software upgrades etc. Why do you want to save in the appdata folder - perhaps I could suggest the best folder above to use.

Upvotes: 7

Related Questions