Skip to content

Commit

Permalink
Update persistent storage example for WPF Framework (#1856)
Browse files Browse the repository at this point in the history
* Update example

* cull some needless comments
  • Loading branch information
adegeo authored Jun 14, 2024
1 parent 205cd48 commit ec78e78
Show file tree
Hide file tree
Showing 22 changed files with 912 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "How to: Persist and Restore Application-Scope Properties Across Application Sessions"
description: Learn how to persist and restore application-scope properties across application sessions via included code examples in XAML, C#, and Visual Basic.
ms.date: "03/30/2017"
ms.date: 06/13/2024
dev_langs:
- "csharp"
- "vb"
Expand All @@ -18,11 +18,17 @@ ms.assetid: 55d5904a-f444-4eb5-abd3-6bc74dd14226

This example shows how to persist application-scope properties when an application shuts down, and how to restore application-scope properties when an application is next launched.

## Example
## Example

The application persists application-scope properties to, and restores them from, isolated storage. Isolated storage is a protected storage area that can safely be used by applications without file access permission. The *App.xaml* file defines the `App_Startup` method as the handler for the <xref:System.Windows.Application.Startup?displayProperty=nameWithType> event, and the `App_Exit` method as the handler for the <xref:System.Windows.Application.Exit?displayProperty=nameWithType> event, as shown in the highlighted lines of the first example. The second example shows a portion of the *App.xaml.cs* and *App.xaml.vb* files that highlights the code for the `App_Startup` method, which restores application-scope properties, and the `App_Exit` method, which saves application-scope properties.
The application persists application-scope properties to, and restores them from, isolated storage. Isolated storage is a protected storage area that can safely be used by applications without file access permission. The *App.xaml* file defines the `App_Startup` method as the handler for the <xref:System.Windows.Application.Startup?displayProperty=nameWithType> event, and the `App_Exit` method as the handler for the <xref:System.Windows.Application.Exit?displayProperty=nameWithType> event, as shown in the highlighted lines of the following XAML:

[!code-xaml[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesXAML1](~/samples/snippets/csharp/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/CSharp/App.xaml?highlight=1-7)]

[!code-csharp[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesCODEBEHIND1](~/samples/snippets/csharp/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/CSharp/App.xaml.cs?highlight=17-55)]
[!code-vb[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesCODEBEHIND1](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/visualbasic/application.xaml.vb?highlight=14-45)]
> [!NOTE]
> The following XAML is written for CSharp. The Visual Basic version omits the class declaration.
:::code language="xaml" source="./snippets/persist-and-restore-application-scope-properties/csharp/App.xaml" highlight="5,6":::

This next example shows the Application code-behind, which contains the event handlers for the XAML. The `App_Startup` method restores application-scope properties, and the `App_Exit` method saves application-scope properties.

:::code language="csharp" source="./snippets/persist-and-restore-application-scope-properties/csharp/App.xaml.cs":::

:::code language="vb" source="./snippets/persist-and-restore-application-scope-properties/vb/Application.xaml.vb":::
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="SDKSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="App_Startup"
Exit="App_Exit">
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows;

namespace SDKSamples
{
public partial class App : Application
{
string _filename = "App.data";

public App()
{
// Initialize application-scope property
Properties["NumberOfAppSessions"] = "0";
}

private void App_Startup(object sender, StartupEventArgs e)
{
// Restore application-scope property from isolated storage
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
try
{
if (storage.FileExists(_filename))
{
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read))
using (StreamReader reader = new StreamReader(stream))
{
// Restore each application-scope property individually
while (!reader.EndOfStream)
{
string[] keyValue = reader.ReadLine().Split(new char[] { ',' });
Properties[keyValue[0]] = keyValue[1];
}
}
}
}
catch (DirectoryNotFoundException ex)
{
// Path the file didn't exist
}
catch (IsolatedStorageException ex)
{
// Storage was removed or doesn't exist
// -or-
// If using .NET 6+ the inner exception contains the real cause
}
}

private void App_Exit(object sender, ExitEventArgs e)
{
// Increase the amount of times the app was opened
Properties["NumberOfAppSessions"] = int.Parse((string)Properties["NumberOfAppSessions"]) + 1;

// Persist application-scope property to isolated storage
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(stream))
{
// Persist each application-scope property individually
foreach (string key in Properties.Keys)
writer.WriteLine("{0},{1}", key, Properties[key]);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window x:Class="SDKSamples.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SDKSamples"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Label Content="Times app was opened: " HorizontalAlignment="Left" Margin="31,80,0,0" VerticalAlignment="Top"/>
<Label Content="X" HorizontalAlignment="Left" Margin="168,80,0,0" VerticalAlignment="Top" x:Name="lblTimes"/>

</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SDKSamples
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
lblTimes.Content = App.Current.Properties["NumberOfAppSessions"];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net481</TargetFramework>
<UseWPF>true</UseWPF>
<RootNamespace>SDKSamples</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

</application>
</compatibility>

<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
-->

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>

</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="App_Startup"
Exit="App_Exit">
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Imports System.IO
Imports System.IO.IsolatedStorage

Class Application

Private _filename As String = "App.data"

Public Sub New()
' Initialize application-scope property
Properties("NumberOfAppSessions") = "0"
End Sub

Private Sub App_Startup(ByVal sender As Object, ByVal e As StartupEventArgs)
' Restore application-scope property from isolated storage
Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()
Try
If storage.FileExists(_filename) Then

Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read)
Using reader As New StreamReader(stream)

' Restore each application-scope property individually
Do While Not reader.EndOfStream
Dim keyValue() As String = reader.ReadLine().Split(New Char() {","c})
Properties(keyValue(0)) = keyValue(1)
Loop

End Using
End Using

End If

Catch ex As DirectoryNotFoundException
' Path the file didn't exist
Catch ex As IsolatedStorageException
' Storage was removed or doesn't exist
' -or-
' If using .NET 6+ the inner exception contains the real cause
End Try
End Sub

Private Sub App_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
'Increase the amount of times the app was opened
Properties("NumberOfAppSessions") = Integer.Parse(DirectCast(Properties("NumberOfAppSessions"), String)) + 1

' Persist application-scope property to isolated storage
Dim storage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForDomain()

Using stream As IsolatedStorageFileStream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write)
Using writer As New StreamWriter(stream)

' Persist each application-scope property individually
For Each key As String In Properties.Keys
writer.WriteLine("{0},{1}", key, Properties(key))
Next key

End Using
End Using
End Sub

End Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Imports System.Windows

'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
'1st parameter: where theme specific resource dictionaries are located
'(used if a resource is not found in the page,
' or application resource dictionaries)

'2nd parameter: where the generic resource dictionary is located
'(used if a resource is not found in the page,
'app, and any theme specific resource dictionaries)
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SDKSamples"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Loaded="Window_Loaded">
<Grid>
<Label Content="Times app was opened: " HorizontalAlignment="Left" Margin="15,376,0,0" VerticalAlignment="Top"/>
<Label Content="X" HorizontalAlignment="Left" Margin="152,376,0,0" VerticalAlignment="Top" x:Name="lblTimes"/>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Public Class MainWindow
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
lblTimes.Content = Application.Current.Properties("NumberOfAppSessions")
End Sub
End Class
Loading

0 comments on commit ec78e78

Please sign in to comment.