<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;
}
}