<metro>读取目录名
首先,我们设计好一个Blank App程序,在添加Button和BlockText控件,在Button中找出Click事件,再单击进入事件。其中xaml中代码显示为:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button x:Name="Folder" Content="Button" HorizontalAlignment="Left" Height="52" Margin="260,124,0,0" VerticalAlignment="Top" Width="183" Click="Folder_Click"/> <TextBlock x:Name="Foutput" HorizontalAlignment="Left" Height="136" Margin="260,225,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="444"/> </Grid>
其次,用FolderPicker类new一个实例folderPicker ,在调用属性 SuggestedStartLocation设定起始位置。接着用FileTypeFilter.Add增加可以识别的文件类型。再用StorageFolder类新建一个实例,等待异步PickSingleFolderAsync方法, 获得一个目录。 通过StorageApplicationPermissions.FutureAccessList.AddOrReplace方法,得到选择的目录权限。最后,将它的目录名显示到BlockText中。按F5演示成功。其中事件中代码如下:
private async void Folder_Click(object sender, RoutedEventArgs e) { FolderPicker folderPicker = new FolderPicker(); folderPicker.SuggestedStartLocation = PickerLocationId.Desktop; folderPicker.FileTypeFilter.Add(".docx"); folderPicker.FileTypeFilter.Add(".xlsx"); folderPicker.FileTypeFilter.Add(".pptx"); StorageFolder folder = await folderPicker.PickSingleFolderAsync(); if (folderPicker != null) { StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder); Foutput.Text = "Picked folder: " + folder.Name; } else { Foutput.Text = "Operation cancelled."; } }