首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Windows 8 應用開發:本地資料存取與檔案讀寫完整指南

     編輯:關於.NET

前言:Windows 8 應用的檔案存取新範式

檔案讀寫對 .NET 開發者而言並不陌生,這是資料儲存與處理的基礎操作。然而在 Windows Store 應用程式開發中,由於安全性與權限機制的限制,我們無法像傳統桌面應用那樣「自由」存取任意路徑。這項限制雖然看似束縛,實則是為了適應行動裝置平台的安全模型——若您需要完整的檔案系統存取權限,傳統桌面應用程式(如 WPF、Windows Forms)或許更適合。

核心概念:應用程式資料容器

在商店應用中,主要操作兩類目錄:應用程式資料資料夾與使用者文件庫。系統提供 ms-appx:// 與 ms-appdata:// 等特殊 URI 配置,但開發者無需死記這些路徑,透過 Windows.Storage.ApplicationData.Current 即可輕鬆取得對應的 StorageFolder 物件。

三大本地儲存區域

  • LocalFolder:本機持久化資料,不隨裝置漫遊,適合儲存大型資料集。
  • RoamingFolder:漫遊資料,會自動同步至使用者登入的其他裝置(有大小限制)。
  • TemporaryFolder:暫存資料,系統可能隨時清理,適合快取檔案。

實作範例一:取得應用程式資料目錄路徑

步驟 1:建立空白應用程式專案

於 Visual Studio 中新增「空白應用程式 (Windows)」專案,名稱為 App1。

步驟 2:設計 UI (MainPage.xaml)

<Page x:Class="App1.MainPage"
      xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:App1"
      xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <RichTextBlock Margin="15">
            <Paragraph FontSize="24">
                <Span>本地儲存目錄:</Span>
                <Run x:Name="rnLocal"/>
                <LineBreak/><LineBreak/>
                <Span>漫遊儲存目錄:</Span>
                <Run x:Name="rnRoaming"/>
                <LineBreak/><LineBreak/>
                <Span>暫存目錄:</Span>
                <Run x:Name="rnTemp"/>
            </Paragraph>
        </RichTextBlock>
    </Grid>
</Page>

步驟 3:後端程式碼 (MainPage.xaml.cs)

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    this.rnLocal.Text = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
    this.rnRoaming.Text = Windows.Storage.ApplicationData.Current.RoamingFolder.Path;
    this.rnTemp.Text = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
}

執行結果將顯示這三個目錄皆位於 %UserProfile%\AppData\Local\Packages\<PackageName> 下,結構清晰且隔離性佳。

應用程式資料目錄路徑示意圖

實作範例二:本地資料夾的檔案讀寫

最簡單的方式是使用靜態類別 Windows.Storage.FileIO 或 PathIO,提供非同步的 WriteTextAsync、ReadTextAsync 等方法。

UI 設計 (MainPage.xaml)

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <TextBox x:Name="txtInput" Margin="13" Grid.Row="0" TextWrapping="Wrap"/>
    <StackPanel Orientation="Horizontal" Margin="14,10" Grid.Row="1">
        <Button FontFamily="Segoe UI Symbol" FontSize="25" Click="onWrite">
            <Button.Content>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="tbSob" Grid.Column="0" Text=""/>
                    <TextBlock Grid.Column="1" Text="寫入檔案"/>
                </Grid>
            </Button.Content>
        </Button>
        <Button Margin="16,0,0,0" FontFamily="Segoe UI Symbol" FontSize="25" Click="onRead">
            <Button.Content>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="從檔案讀取"/>
                    <TextBlock Grid.Column="1" Text="{Binding ElementName=tbSob,Path=Text}" RenderTransformOrigin="0.5,0.5">
                        <TextBlock.RenderTransform>
                            <RotateTransform Angle="180"/>
                        </TextBlock.RenderTransform>
                    </TextBlock>
                </Grid>
            </Button.Content>
        </Button>
    </StackPanel>
    <TextBlock x:Name="txtRead" Grid.Row="2" Margin="15" TextWrapping="Wrap" FontSize="24"/>
</Grid>

程式碼實作 (MainPage.xaml.cs)

private async void onWrite(object sender, RoutedEventArgs e)
{
    if (string.IsNullOrWhiteSpace(this.txtInput.Text)) return;
    var local = Windows.Storage.ApplicationData.Current.LocalFolder;
    var myFile = await local.CreateFileAsync("my.txt");
    if (myFile != null)
    {
        await Windows.Storage.FileIO.WriteTextAsync(myFile, this.txtInput.Text);
        var dlg = new Windows.UI.Popups.MessageDialog("寫入成功。");
        await dlg.ShowAsync();
    }
}

private async void onRead(object sender, RoutedEventArgs e)
{
    string msg = string.Empty;
    try
    {
        var local = Windows.Storage.ApplicationData.Current.LocalFolder;
        var file = await local.GetFileAsync("my.txt");
        if (file != null)
        {
            this.txtRead.Text = await Windows.Storage.FileIO.ReadTextAsync(file);
            msg = "讀取完成。";
        }
    }
    catch (Exception ex) { msg = ex.Message; }
    var dlg = new Windows.UI.Popups.MessageDialog(msg);
    await dlg.ShowAsync();
}
檔案讀寫執行結果

進階主題:存取使用者文件庫

若需存取「我的文件」、「圖片」、「音樂」等使用者已知資料夾,可使用 Windows.Storage.KnownFolders 類別。但不同於應用程式資料區,這裡涉及使用者隱私,必須在封裝清單 (Package.appxmanifest) 中宣告權限:

  1. 於「功能」頁籤勾選「文件庫」。
  2. 於「宣告」頁籤新增「檔案類型關聯」,指定應用可處理的副檔名(如 .tmd)。

範例:將文字內容儲存至文件庫

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel Margin="20">
        <TextBox x:Name="txtInput" TextWrapping="Wrap" Height="320" FontSize="20"/>
        <Button Content="儲存到文件庫" Margin="8,10,0,0" Padding="12,7" FontSize="25" Click="onSaveDoc"/>
    </StackPanel>
</Grid>
private async void onSaveDoc(object sender, RoutedEventArgs e)
{
    if (string.IsNullOrWhiteSpace(this.txtInput.Text)) return;
    var myDocuments = Windows.Storage.KnownFolders.DocumentsLibrary;
    var tmdFile = await myDocuments.CreateFileAsync("Data.tmd");
    if (tmdFile != null)
    {
        string strInfo = string.Empty;
        try
        {
            await Windows.Storage.FileIO.WriteTextAsync(tmdFile, this.txtInput.Text);
            strInfo = "已寫入文件。";
        }
        catch (Exception ex) { strInfo = ex.Message; }
        var dlg = new Windows.UI.Popups.MessageDialog(strInfo);
        await dlg.ShowAsync();
    }
}
封裝清單功能設定 檔案類型關聯宣告 執行成功畫面 文件內容驗證

總結

Windows 8 應用的檔案存取雖有沙箱限制,但透過 ApplicationData 與 KnownFolders API,開發者仍能安全、直觀地完成本地持久化、漫遊同步及使用者文件庫互動。掌握非同步模式與清單宣告機制,是開發高品質 Store 應用的關鍵。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved