Windows Phone 學習 創建和刪除文件夾

  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Orientation="Vertical">
                <TextBlock>輸入文件夾的名稱:</TextBlock>
                <TextBox Name="txtDirName"/>
                <Button Content="創建"
                            Click="OnCreate"/>
            </StackPanel>
            <ListBox Name="dirList" Grid.Row="1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0"
                                       Text="{Binding}"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       Margin="0,0,20,0"
                                       FontSize="32"/>
                            <Button Content="刪除"
                                    Grid.Column="1"
                                    Tag="{Binding}"
                                    Click="OnDirDelete_Click"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    private void OnCreate(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.txtDirName.Text))
            {
                MessageBox.Show("請輸入一個有效的目錄名。");
                return;
            }
            using (IsolatedStorageFile iso =IsolatedStorageFile.GetUserStoreForApplication())
            {
                // 先判斷一下目錄是否已存在
                // 如果不存在則創建新目錄
                if (iso.DirectoryExists(this.txtDirName.Text) == false)
                {
                    iso.CreateDirectory(txtDirName.Text);
                    // 更新ListBox中的顯示
                    SetDirList();
                    this.txtDirName.Text = "";
                }
            }
        }

        private void OnDirDelete_Click(object sender, RoutedEventArgs e)
        {
            Button btn = e.OriginalSource as Button;
            if (btn != null)
            {
                // 從Tag屬性中取得目錄名
                string dirName = (string)btn.Tag;
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if (!iso.DirectoryExists(dirName))
                {
                    return;
                }
                if (MessageBox.Show("確定要刪除目錄嗎?","", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    // 刪除目錄
                    try
                    {
                        iso.DeleteDirectory(dirName);
                        // 更新ListBox中的顯示
                        SetDirList();
                    }
                    catch (IsolatedStorageException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                iso.Dispose();
            }
        }

        private void SetDirList()
        {
            using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                // 獲取獨立存儲中的所有根目錄
                string[] dirs = iso.GetDirectoryNames();
                this.dirList.ItemsSource = dirs;
            }
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章