Hello again! I like writing articles based on my personal experiences and I like giving my own personal opinions on many things concerning Microsoft. Today's article is no exception. Being curios, I like to explore the different realms between all of the different programming worlds. I like comparing them. I like pulling them apart and pasting them back together.
Coming from a desktop programming background, I realize that I need to change my outlook and shift my comfort zone (my focus) to mobile apps and Windows Store apps, in case an opportunity should come my way. Today's topic will focus on a desktop concept (the registry) that is not present in a Windows Store app. Why not? Well, it is because a Windows Store app differs entirely from a desktop app.
Windows Store apps do not have direct access to the registry. Similar to an earlier article I wrote (Serialization in Windows Store Apps) Windows Store apps has limited capabilities because it is not a desktop app that resides on the user's computer as normal desktop apps do. It does not have access to the whole system, as we are accustomed to. It simply does not function as a normal Desktop app we are used to. Get it? Good. Let us create today's project to see how we can still store data similar to how a desktop app would store pieces of information in the registry.
Open Visual Studio 2012 or Visual Studio 2013 and create a Visual Basic Windows Store app. I have named mine Registry_Ex_VB. You could obviously use any name you like. On the Main Page, add three buttons and a Textblock (as reflected in Figure 1).
Figure 1 - Design
The resulting XAML code would look like the following:
<Button x:Name="btDelete" Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="218,44,0,0" Grid.Row="1"/> <Button x:Name="btRead" Content="Read" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="120,44,0,0" Grid.Row="1"/> <Button x:Name="btWrite" Content="Write" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="26,44,0,0" Grid.Row="1"/> <TextBlock x:Name="tbValue" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Row="1" Margin="38,115,0,0" Height="28" Width="242" FontSize="20"/>
Windows 8 Store apps cannot access the registry. We can however use the following two APIs:
We will work with these now in our application.
As always, let us add the necessary Imports statements (I am predictable, aren't I?) :
This gives us access to all the Storage functionalities necessary for our app. Add the following member variable (so that we can keep track of what was stored and what needs to be read):
Private adcLocalStorage As ApplicationDataContainer = _ ApplicationData.Current.LocalSettings 'Create a Local Settings Object
Add the next code segment behind the Write button:
'Write Info To Data Container Private Sub btWrite_Click(sender As Object, e As RoutedEventArgs) Handles btWrite.Click 'Create Main Data Container Dim adcMainContainer As ApplicationDataContainer = _ adcLocalStorage.CreateContainer("Reg_Example", ApplicationDataCreateDisposition.Always) 'Always Create Container 'If Main Storage Key Present If adcLocalStorage.Containers.ContainsKey("Reg_Example") Then 'Create A Setting And Store Data Inside It adcLocalStorage.Containers("Reg_Example").Values("Reg_Setting") = "Hello Hannes" 'Data To Be Stored End If End Sub
Here, we create a DataContainer object named Reg_Example. We specify that we will always need this setting to be created. This is nice, because we will not have to worry to test whether or not this container already exists or not - although you can. This is just a small example for you to build on. We then store the "Hello Hannes" string into the datacontainer's value Reg_Setting.
Add the next code segment to read this data back into our Windows Store app:
'Read Info From Data Container Private Sub btRead_Click(sender As Object, e As RoutedEventArgs) Handles btRead.Click 'Determine If Container Is Present Dim blnContainer As Boolean = adcLocalStorage.Containers.ContainsKey("Reg_Example") Dim blnSetting As Boolean = False 'Determine If Setting Is Present If blnContainer Then 'If Container Present 'If Setting Present Inside Container blnSetting = adcLocalStorage.Containers("Reg_Example").Values.ContainsKey("Reg_Setting") 'Get Setting Values And Display In TextBox tbValue.Text = adcLocalStorage.Containers("Reg_Example").Values("Reg_Setting") End If End Sub
Inside the Read button, we quickly make sure that the key Reg_Example does in fact exist; we then check if it has a valid setting that we can read. After all of this, we simply read the setting into our app and display it in a textblock.
Add the last piece of the puzzle (so to speak):
'Delete Data Container Private Sub btDelete_Click(sender As Object, e As RoutedEventArgs) Handles btDelete.Click adcLocalStorage.DeleteContainer("Reg_Example") 'Delete End Sub
This deletes the specified datacontainer object.
When run, your app should resemble Figure 2.
Figure 2 - Running the app
I am including the source files below, just in case you have missed a step or two.
That about wraps things up for us today! Let me move on to newer topics to write about, until then, cheers and good luck!
About the Author:
Hannes du Preez is a Microsoft MVP for Visual Basic for the sixth year in a row. He is a trainer at a South African-based company providing IT training in the Vaal Triangle. You could reach him at hannes [at] ncc-cla [dot] com.