在任何应用程序中,最重要的是数据之一.如果您是 .net 开发人员,您可能知道隔离存储,并且通过通用Windows平台(UWP)应用程序遵循相同的概念.
这些是您的应用程序可以访问数据的区域.该应用程序包含一些区域,该区域是该特定应用程序专用的,其他区域无法访问,但还有许多其他区域,您可以在其中存储和保存数据.
以下是每个文件夹的简要说明.
S.No. | Folder&说明 |
---|---|
1 | App package folder 包管理器将所有应用程序的相关文件安装到App包文件夹中,app只能读取该文件夹中的数据. |
2 | Local folder 应用程序将本地数据存储到本地文件夹中.它可以存储数据达到存储设备的限制. |
3 | Roaming folder 与应用程序相关的设置和属性存储在漫游文件夹中.其他设备也可以访问此文件夹中的数据.每个应用程序的大小限制为100KB. |
4 | Temp Folder 临时存储的使用,并且无法保证在您的应用程序再次运行时它仍然可用. |
5 | Publisher Share 来自同一发布商的所有应用的共享存储空间.它在app manifest中声明. |
6 | Credential Locker 用于密码凭证对象的安全存储. |
7 | OneDrive OneDrive是免费在线存储您的Microsoft帐户附带. |
8 | Cloud 在云上存储数据. |
9 | Known folders 这些文件夹已知的文件夹,例如My图片,视频和音乐. |
10 | 可移动存储 USB存储设备或外置硬盘等. |
在Windows 8中,引入了新的API用于文件处理.这些API位于 Windows.Storage 和 Windows.Storage.Streams 命名空间中.您可以使用这些API而不是 System.IO.IsolatedStorage 命名空间.通过使用这些API,可以更轻松地将Windows Phone应用程序移植到Windows应用商店,并且可以轻松地将应用程序升级到Windows的未来版本.
访问本地漫游或临时文件夹,你需要调用这些API :
StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFolder roamingFolder = ApplicationData.Current.RoamingFolder; StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
要在本地文件夹中创建新文件,请使用以下代码 :
StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile textFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
这是打开新创建的文件并在该文件中写入一些内容的代码.
using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite)) { using (DataWriter textWriter = new DataWriter(textStream)){ textWriter.WriteString(contents); await textWriter.StoreAsync(); } }
您可以从本地文件夹再次打开相同的文件,如给出的代码所示下面.
using (IRandomAccessStream textStream = await textFile.OpenReadAsync()) { using (DataReader textReader = new DataReader(textStream)){ uint textLength = (uint)textStream.Size; await textReader.LoadAsync(textLength); contents = textReader.ReadString(textLength); } }
要了解数据的读写是如何工作的,让我们一起来看看一个简单的例子.下面给出了添加了不同控件的XAML代码.
<Page x:Class = "UWPFileHandling.MainPage" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local = "using:UWPFileHandling" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable = "d"> <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Button x:Name = "readFile" Content = "Read Data From File" HorizontalAlignment = "Left" Margin = "62,518,0,0" VerticalAlignment = "Top" Height = "37" Width = "174" Click = "readFile_Click"/> <TextBox x:FieldModifier = "public" x:Name = "textBox" HorizontalAlignment = "Left" Margin = "58,145,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Height = "276" Width = "245"/>. <Button x:Name = "writeFile" Content = "Write Data to File" HorizontalAlignment = "Left" Margin = "64,459,0,0" VerticalAlignment = "Top" Click = "writeFile_Click"/> <TextBlock x:Name = "textBlock" HorizontalAlignment = "Left" Margin = "386,149,0,0" TextWrapping = "Wrap" VerticalAlignment = "Top" Height = "266" Width = "250" Foreground = "#FF6231CD"/> </Grid> </Page>
下面给出了不同事件的C#实现,以及用于读取和写入文本数据的 FileHelper 类的实现file.
using System; using System.IO; using System.Threading.Tasks; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace UWPFileHandling { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public partial class MainPage : Page { const string TEXT_FILE_NAME = "SampleTextFile.txt"; public MainPage(){ this.InitializeComponent(); } private async void readFile_Click(object sender, RoutedEventArgs e) { string str = await FileHelper.ReadTextFile(TEXT_FILE_NAME); textBlock.Text = str; } private async void writeFile_Click(object sender, RoutedEventArgs e) { string textFilePath = await FileHelper.WriteTextFile(TEXT_FILE_NAME, textBox.Text); } } public static class FileHelper { // Write a text file to the app's local folder. public static async Task<string> WriteTextFile(string filename, string contents) { StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile textFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite)){ using (DataWriter textWriter = new DataWriter(textStream)){ textWriter.WriteString(contents); await textWriter.StoreAsync(); } } return textFile.Path; } // Read the contents of a text file from the app's local folder. public static async Task<string> ReadTextFile(string filename) { string contents; StorageFolder localFolder = ApplicationData.Current.LocalFolder; StorageFile textFile = await localFolder.GetFileAsync(filename); using (IRandomAccessStream textStream = await textFile.OpenReadAsync()){ using (DataReader textReader = new DataReader(textStream)){ uint textLength = (uint)textStream.Size; await textReader.LoadAsync(textLength); contents = textReader.ReadString(textLength); } } return contents; } } }
编译并执行上述代码时,您将看到以下窗口.
现在,你在文本框中写了一些东西然后点击"将数据写入文件"按钮.程序会将数据写入本地文件夹中的文本文件中.如果单击"从文件读取数据"按钮,程序将从同一文本文件中读取数据,该文件位于本地文件夹中,并将显示在文本块中.