I have translated many documents or books into "independent storage". However, I thought about it and decided to translate isolatedstorage into "isolated storage ", I think this will make it easier for you to understand this concept.
According to the inherent habits of isolated storage, I don't want to make too many theoretical explanations. Theoretically, things are easy to complicate simple things, even if you say how perfect your theoretical knowledge is, I believe everyone is not interested in it. Even if you are interested, you will be confused.
Isolated storage is not unique to WP. It also exists in Silverlight or WPF. More accurately, "independent storage" is available in. NET 2.0 has already appeared. Maybe you didn't notice it, don't believe it? You can find it in the. NET class library.
It doesn't matter if you haven't paid attention to it before. Isolated storage is actually very simple. To put it bluntly, it is the Directory and file management on Windows Phone. on Windows platform, these operations are believed to have been done. net Development friends are certainly very familiar.
The directory and file management methods of Windows Phone are different from those of Windows CE or Windows Mobile in the past. In the past, relative paths were used on both operating systems, and their operation methods were similar to those on PC systems; WP is different. Although relative paths are also used, the operation methods and principles are different.
This is why I want to translate it into "isolated storage". In this way, you can literally guess its features. Each application can only access its independent storage space, you cannot access the directories and structures of other applications or the directories and files of the basic operating system, which greatly improves security.
Okay. Now we only need a simple example and you will understand it.
The example allows you to enter a directory name and click "CREATE". A directory will be created in the isolated storage, and then click the second button to check whether the directory exists, the third button is used to delete a directory.
[HTML]View plaincopyprint?
- <Phone: phoneapplicationpage
- X: class = "isostoragesample1.mainpage"
- Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
- Xmlns: Phone = "CLR-namespace: Microsoft. Phone. controls; Assembly = Microsoft. Phone"
- Xmlns: shell = "CLR-namespace: Microsoft. Phone. Shell; Assembly = Microsoft. Phone"
- Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
- Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
- MC: ignorable = "D" D: designwidth = "480" D: designheight = "768"
- Fontfamily = "{staticresource phonefontfamilynormal }"
- Fontsize = "{staticresource phonefontsizenormal }"
- Foreground = "{staticresource phoneforegroundbrush }"
- Supportedorientations = "portrait" orientation = "portrait"
- Shell: systemtray. isvisible = "true">
- <! -- Layoutroot is the root mesh that contains all page content -->
- <Grid X: Name = "layoutroot" background = "Transparent">
- <Grid. rowdefinitions>
- <Rowdefinition Height = "Auto"/>
- <Rowdefinition Height = "*"/>
- </Grid. rowdefinitions>
- <! -- Titlepanel contains the application name and page title -->
- <Stackpanel X: Name = "titlepanel" grid. Row = "0" margin = ",">
- <Textblock X: Name = "applicationtitle" text = "My applications" style = "{staticresource phonetextnormalstyle}"/>
- <Textblock X: Name = "pagetitle" text = "isolated storage" margin = "9,-7,0, 0" style = "{staticresource phonetexttitle1style}"/>
- </Stackpanel>
- <! -- Contentpanel-place other content here -->
- <Grid X: Name = "contentpanel" grid. Row = "1" margin = "12,0, 12,0">
- <Textbox Height = "72" horizontalalignment = "Left" margin = "289," name = "txtdirname" verticalalignment = "TOP" width = ""/>
- <Button content = "CREATE" Height = "72" horizontalalignment = "Left" margin = "307,25, 127 "name =" btncreate "verticalignment =" TOP "width =" "Click =" btncreate_click "/>
- <Button content = "check if the directory already exists" Height = "72" horizontalalignment = "Left" margin = "146,120, 276 "name =" btncheck "verticalignment =" TOP "width =" "Click =" btncheck_click "/>
- <Button content = "delete directory" Height = "72" horizontalalignment = "Left" margin = "0,283, 422 "name =" btndel "verticalignment =" TOP "width =" "Click =" btndel_click "/>
- <Textblock Height = "38" horizontalalignment = "Left" margin = "9,138, 131" name = "tbdisplay" verticalalignment = "TOP" width = ""/>
- </GRID>
- </GRID>
- </Phone: phoneapplicationpage>
[CSHARP]View plaincopyprint?
- Private void btncreate_click (Object sender, routedeventargs E)
- {
- // You can use the getuserstoreforapplication static method to return an isolatedstoragefile instance.
- Isolatedstoragefile ISO = isolatedstoragefile. getuserstoreforapplication ();
- Try
- {
- If (ISO. directoryexists (txtdirname. Text ))
- {
- Return;
- }
- Iso.createdirectory(this.txt dirname. Text );
- }
- Catch
- {MessageBox. Show ("error! ");}
- }
- Private void btncheck_click (Object sender, routedeventargs E)
- {
- Isolatedstoragefile ISO = isolatedstoragefile. getuserstoreforapplication ();
- Bool exists = ISO. directoryexists (txtdirname. Text );
- If (exists)
- {
- This. tbdisplay. Text = "already exists ";
- }
- Else
- {
- This. tbdisplay. Text = "nonexistent ";
- }
- }
- Private void btndel_click (Object sender, routedeventargs E)
- {
- Isolatedstoragefile ISO = isolatedstoragefile. getuserstoreforapplication ();
- Try
- {
- ISO. deletedirectory (txtdirname. Text );
- }
- Catch
- {
- MessageBox. Show ("error! ");
- }
- }
Note that the system. Io. isolatedstorage namespace should be introduced.