1、选择单个文件,FileTypeFilter.Add是添加能访问的文件类型。
private
async
void
filePickerBtn_Click(
object
sender, RoutedEventArgs e)
{
FileOpenPicker fileOpenPicker =
new
FileOpenPicker();
fileOpenPicker.FileTypeFilter.Add(
".jpg"
);
fileOpenPicker.FileTypeFilter.Add(
".jpeg"
);
fileOpenPicker.FileTypeFilter.Add(
".png"
);
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
if
(file !=
null
)
{
displayTextBlock.Text = file.Name;
}
}
|
2、选择多个文件,并且是任意文件类型
private
async
void
filePickerBtn_Click(
object
sender, RoutedEventArgs e)
{
FileOpenPicker fileOpenPicker =
new
FileOpenPicker();
fileOpenPicker.FileTypeFilter.Add(
"*"
);
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
IReadOnlyList<StorageFile> files = await fileOpenPicker.PickMultipleFilesAsync();
if
(files.Count > 0)
{
foreach
(StorageFile file
in
files)
{
displayTextBlock.Text += file.Name +
"\n"
;
}
}
}
|
3、 选择文件夹
private
async
void
folderPickerBtn_Click(
object
sender, RoutedEventArgs e)
{
if
(EnsureUnsnapped())
{
FolderPicker folderPicker =
new
FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add(
"*"
);
StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();
if
(
null
!= storageFolder)
{
displayTextBlock.Text = storageFolder.Name;
}
}
}
|
FilePicker APIs是不能在snapped状态下使用
bool
EnsureUnsnapped()
{
//FilePicker APIs不能再snapped状态下使用。
return
((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
}
|
4、保存文件
private
async
void
SaveFileBtn_Click(
object
sender, RoutedEventArgs e)
{
if
(EnsureUnsnapped())
{
FileSavePicker savePicker =
new
FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.FileTypeChoices.Add(
"Text"
,
new
List<
string
>() {
".txt"
});
savePicker.FileTypeChoices.Add(
"Word Docment"
,
new
List<
string
>() {
".docx"
,
".doc"
});
savePicker.DefaultFileExtension =
".txt"
;
savePicker.SuggestedFileName =
"New Document"
;
StorageFile storageFile = await savePicker.PickSaveFileAsync();
if
(
null
!= storageFile)
{
displayTextBlock.Text = storageFile.Name;
}
}
}
|
选择保存的文件保存自己要求的文件。自定义文件类型和文件名。
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/03/27/2400416.html,如需转载请自行联系原作者