Search code examples
c#.netwindows-store-appsdesktop-bridgemsix

CommonApplicationData folder read-only after using MSIX Packaging Tool


I have written a .Net Windows Forms application that uses the common application data folder to store logfiles and user accounts. The application is distributed using an install shield project and runs perfect on all different Windows versions.

Some parts of the code from different files is shown below

// Defining the path to use (in ProductInfo class)
public static string CommonApplicationDataPath
{
    get
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
        path = StringHelper.EnsureEndsWithSlash(path);
        path += Vendor + @"\" + ProductName + @"\";

        return path;
    }
}

// Configuring the logger and user manager instances at startup
Logger.Configure(string.Empty, ProductInfo.Password, ProductInfo.CommonApplicationDataPath);
UserManager.Configure(User.Empty, ProductInfo.Password, ProductInfo.CommonApplicationDataPath, 
    ProductInfo.UserLimitCount);

// Example method for saving the users to file (in UserManager class)
public bool SaveUsers(AppUsers appUsers)
{
    AppUsersSerializer serializer = new AppUsersSerializer(_password, _fileName);

    if (serializer.Serialize(appUsers) == true)
    {
        return true;
    }
    else
    {
        Logger.Instance.Log(Logs.ErrorB.UserSave, _fileName);
        return false;
    }
}

I would now like to publish the application via Windows Store and have used the MSIX Packaging Tool. To sign the package I have created a self signed certificate and added it to the Trusted Root Certificate Authorities. The .msix package is install on the same PC as my old desktop version of the app.

The problem I have is that the application is not able to write to the files located in the CommonApplicationData folder. The application can read and load the data, but not update and write the changes to the files. Thus, the path to the files is correct, but some write permission seems to be missing. I have tried different capabilities on the package and even ticked all, but without any effect.

I have also browsed to the C:\Program Files\WindowsApps\<my app package>\ folder and checked the structure of the application and located the files. They are there, but only readable for the app. Removing the files will not create new ones when they should be added as done in the old desktop Windows Forms version.

The application is quite big and contains lots of functionality which runs great in the Windows Store app context. The only missing piece is the above mentioned issues with the file writing.

Any advice would be really appreciated.


Solution

  • After some continued searching on different websites I came across a viable solution for my issue.

    The Microsoft MSDN blog described how to use the different folders in an appropriate way.

    http://blogs.msdn.microsoft.com/appconsult/2017/03/06/handling-data-in-a-converted-desktop-app-with-the-desktop-bridge/

    The proposed solution is to change:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

    to:

    string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

    It will place the files in the user's local directory, meaning the data will be available only for the current user. Sharing the same log file and user accounts between different users of the application, will thus not be possible but that is ok for now.

    You may also need to make sure that the folder exists: C:\Users\<user>\AppData\Local\<vendor>\<product> because it might not always be created during installation of your application. It depends if it has user specific settings or not.