<metro>读取文件名

首先,我们打开VS2012,选择模板 Blank App,填好名字点击进入。在MainPage.xaml中选择一个Button和TextBlock 控件。可以在button中选择事件Click。在xaml中代码如下:

 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button x:Name="File" Content="Button" HorizontalAlignment="Left" Height="60" Margin="284,116,0,0" VerticalAlignment="Top" Width="203" Click="File_Click"/>
        <TextBlock x:Name="ftxt" HorizontalAlignment="Left" Margin="284,207,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="179" Width="402"/>
    </Grid>

其次,在事件中,用FileOpenPicker类new一个,在一步步用FileTypeFilter选择文件类型。接着,用 StorageFile获取文件,并将名字读到TextBlock中。

最后,按F5运行。我们就可以选择定好的文件的类型,读出文件名。在事件中代码如下:  

 private async void File_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
            openPicker.FileTypeFilter.Add(".txt");
            openPicker.FileTypeFilter.Add(".dps");
            openPicker.FileTypeFilter.Add(".xlsx");
            openPicker.FileTypeFilter.Add(".docx");

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null)
            {
                ftxt.Text = "Picked file: " + file.Name;
            }
            else
            {
                ftxt.Text = "Operation cancelled.";
            }
        }

 

 

        

posted @ 2012-10-31 11:12  伍锋  阅读(203)  评论(0编辑  收藏  举报