Metro style app 文件、文件夹的选择、文件的保存。

本文提供了一个详细的指南,介绍了如何使用C#和C++进行文件及文件夹的选择与保存操作。包括选择单个文件、多个文件、文件夹的方法以及如何保存文件的具体步骤。提供了丰富的代码示例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 Metro style app 文件、文件夹的选择、文件的保存。

 

1、选择单个文件

public async void PickAFile()
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    StorageFile file = await openPicker.PickSingleFileAsync();
    if (file != null)
    {
        // Application now has read/write access to the picked file
        msg = "Picked photo: " + file.Name;
    }
    else
    {
        msg = "Operation cancelled.";
    }
}

 

 

 

2、选择多个文件

public async void PickFiles()
{
    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.List;
    openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    openPicker.FileTypeFilter.Add("*");
    IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
    if (files.Count > 0)
    {
        StringBuilder output = new StringBuilder("Picked files:\n");
        // Application now has read/write access to the picked file(s)
        foreach (StorageFile file in files)
        {
            output.Append(file.Name + "\n");
        }
        msg = output.ToString();
    }
}

 

3、选择文件夹

public async void PickFolder()
{
    FolderPicker folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.FileTypeFilter.Add(".docx");
    folderPicker.FileTypeFilter.Add(".xlsx");
    folderPicker.FileTypeFilter.Add(".pptx");
    folderPicker.FileTypeFilter.Add(".jpg");
    folderPicker.FileTypeFilter.Add(".jpeg");
    StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    if (folder != null)
    {
        // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
        msg = "Picked folder: " + folder.Name;
    }
    else
    {
        msg = "Operation cancelled.";
    }
 
}

 

 

4、保存文件

public async void SaveFile()
{
    FileSavePicker savePicker = new FileSavePicker();
    savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
    // Dropdown of file types the user can save the file as
    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    // Default file name if the user does not type one in or select a file to replace
    savePicker.SuggestedFileName = "New Document";
    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
    {
        // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
        CachedFileManager.DeferUpdates(file);
        // write to file
        await FileIO.WriteTextAsync(file, file.Name);
        // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
        // Completing updates may require Windows to ask for user input.
        FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
        if (status == FileUpdateStatus.Complete)
        {
            msg = "File " + file.Name + " was saved.";
        }
        else
        {
            msg = "File " + file.Name + " couldn't be saved.";
        }
    }
}

 

下面是C++版本

1、选择单个文件 

void BasicHandleCPP::MainPage::PickerAFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    FileOpenPicker^ openPicker = ref new FileOpenPicker();
    openPicker->ViewMode = PickerViewMode::Thumbnail;
    openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    openPicker->FileTypeFilter->Append(".jpg");
    openPicker->FileTypeFilter->Append(".jpeg");
    openPicker->FileTypeFilter->Append(".png");
 
    create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
    {
        if (file)
        {
            Display->Text = "Picked photo: " + file->Name;
        }
        else
        {
            Display->Text = "Operation cancelled.";
        }
    });
 
}

 

 

2、选择多个文件

void BasicHandleCPP::MainPage::PickerFiles_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    FileOpenPicker^ openPicker = ref new FileOpenPicker();
    openPicker->ViewMode = PickerViewMode::List;
    openPicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
    openPicker->FileTypeFilter->Append("*");
 
    create_task(openPicker->PickMultipleFilesAsync()).then([this](IVectorView<StorageFile^>^ files)
    {
        if (files->Size > 0)
        {
            String^ output = "Picked files:\n";
            std::for_each(begin(files), end(files), [this, &output](StorageFile ^file)
            {
                output += file->Name + "\n";
            });
            Display->Text = output;
        }
        else
        {
            Display->Text = "Operation cancelled.";
        }
    });
}

 

 

3、选择文件夹

void BasicHandleCPP::MainPage::PickerFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    FolderPicker^ folderPicker = ref new FolderPicker();
    folderPicker->SuggestedStartLocation = PickerLocationId::Desktop;
 
    // Users expect to have a filtered view of their folders depending on the scenario.
    // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
    folderPicker->FileTypeFilter->Append(".docx");
    folderPicker->FileTypeFilter->Append(".xlsx");
    folderPicker->FileTypeFilter->Append(".pptx");
 
    create_task(folderPicker->PickSingleFolderAsync()).then([this](StorageFolder^ folder)
    {
        if (folder)
        {
            Display->Text = "Picked folder: " + folder->Name;
        }
        else
        {
            Display->Text = "Operation cancelled.";
        }
    });
}

 

4、保存文件

void BasicHandleCPP::MainPage::SaveFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    FileSavePicker^ savePicker = ref new FileSavePicker();
    savePicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
 
    auto plainTextExtensions = ref new Platform::Collections::Vector<String^>();
    plainTextExtensions->Append(".txt");
    savePicker->FileTypeChoices->Insert("Plain Text", plainTextExtensions);
    savePicker->SuggestedFileName = "New Document";
 
    create_task(savePicker->PickSaveFileAsync()).then([this](StorageFile^ file)
    {
        if (file != nullptr)
        {
 
            // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
            CachedFileManager::DeferUpdates(file);
            // write to file
            create_task(FileIO::WriteTextAsync(file, file->Name)).then([this, file]()
            {
                // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                // Completing updates may require Windows to ask for user input.
                create_task(CachedFileManager::CompleteUpdatesAsync(file)).then([this, file](FileUpdateStatus status)
                {
                    if (status == FileUpdateStatus::Complete)
                    {
                        Display->Text = "File " + file->Name + " was saved.";
                    }
                    else
                    {
                        Display->Text = "File " + file->Name + " couldn't be saved.";
                    }
                });
            });
        }
        else
        {
            Display->Text = "Operation cancelled.";
        }
    });
}

 

 

 

 

 

 

 本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/06/28/2567009.html,如需转载请自行联系原作者

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值