Skip to content

LocalSettings throws an UnauthorizedAccessException after AppInstance.Restart #3957

@sjb-sjb

Description

@sjb-sjb

Describe the bug

LocalSettings throws an UnauthorizedAccessException after AppInstance.Restart.
How can I fix this?

Expected: should be able to access LocalSettings after a restart.

Steps to reproduce the bug

Using Windows 10 and Windows App SDK, create a WinUI 3 desktop application using the template.

Change MainWindow.xaml to contain this:

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock x:Name="TimeTextBlock">(unkown)</TextBlock>
    <Button x:Name="SetButton" Click="Set_Click">Set time</Button>
    <Button x:Name="GetButton" Click="Get_Click">Get time</Button>
    <Button x:Name="RestartButton" Click="Restart_Click">Restart</Button>
    <TextBlock x:Name="StatusTextBlock">Startup</TextBlock>
</StackPanel>

Change MainWindow.xaml.cs to contain this:

    public MainWindow()
    {
        this.InitializeComponent();
    }

    private Windows.Storage.ApplicationDataContainer LocalSettings => this._localSettings ??= Windows.Storage.ApplicationData.Current.LocalSettings;
    private Windows.Storage.ApplicationDataContainer? _localSettings;

    private void Set_Click(object sender, RoutedEventArgs args)
    {
        try {
            DateTimeOffset now = DateTimeOffset.Now;
            this.LocalSettings.Values["Time"] = now;
            this.StatusTextBlock.Text = "Set value " + now.ToString();
        } catch (Exception e) {
            this.StatusTextBlock.Text = $"Exception when setting value, type '{e.GetType().Name}' message '{e.Message}'.";
        }
    }

    private void Get_Click(object sender, RoutedEventArgs args)
    {
        try {
            DateTimeOffset? time;
            if (this.LocalSettings.Values.TryGetValue("Time", out object? value)) {
                time = (DateTimeOffset)value;
            } else {
                time = null;
            }
            this.TimeTextBlock.Text = time?.ToString() ?? "(null)";
            this.StatusTextBlock.Text = "Got value shown above from LocalSettings";
        } catch (Exception e) {
            this.StatusTextBlock.Text = $"Exception when getting value, type '{e.GetType().Name}' message '{e.Message}'.";
        }
    }

    private void Restart_Click(object sender, RoutedEventArgs e)
    {
        AppRestartFailureReason failure = Microsoft.Windows.AppLifecycle.AppInstance.Restart("");
        this.StatusTextBlock.Text = $"Restart failed, '{failure.ToString()}'";
    }

Run the app. Click on the Get and Set buttons, observe it works.
Then click on Restart. After the application restart, click on Get or Set and receive an UnauthorizedAccessException.

Expected behavior

Should be able to access LocalSettings after a restart.

Screenshots

No response

NuGet package version

None

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 22H2 (19045, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions