//创建文件 using namespace Windows::Storage; MainPage^ rootPage;rootPage = MainPage::Current; create_task(KnownFolders::DocumentsLibrary->CreateFileAsync(rootPage->Filename, CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file){rootPage->SampleFile = file;OutputTextBlock->Text = "The file '" + file->Name + "' was created.";});
static property Platform::String^ Filename
{
Platform::String^ get()
{
return ref new Platform::String(L"sample.dat");
}
}
property Windows::Storage::StorageFile^ SampleFile
{
Windows::Storage::StorageFile^ get()
{
return sampleFile;
}
void set(Windows::Storage::StorageFile^ value)
{
sampleFile = value;
}
}
2、向文件中写入信息----写Text内容
//另加空间名using namespace concurrency; StorageFile^ file = rootPage->SampleFile;//刚才创建的文件if (file != nullptr){String^ userContent = InputTextBox->Text;if (userContent != nullptr && !userContent->IsEmpty()){create_task(FileIO::WriteTextAsync(file, userContent)).then([this, file, userContent](task<void> task){try{task.get();OutputTextBlock->Text = "The following text was written to '" + file->Name + "':\n\n" + userContent;}catch(COMException^ ex){rootPage->HandleFileNotFoundException(ex);}});}else{OutputTextBlock->Text = "The text box is empty, please write something and then click 'Write' again.";}}
//读文件内容-----读text内容
rootPage->ResetScenarioOutput(OutputTextBlock);StorageFile^ file = rootPage->SampleFile;if (file != nullptr){String^ userContent = InputTextBox->Text;if (userContent != nullptr && !userContent->IsEmpty()){IBuffer^ buffer = GetBufferFromString(userContent);create_task(FileIO::WriteBufferAsync(file, buffer)).then([this, file, buffer, userContent](task<void> task){try{task.get();OutputTextBlock->Text = "The following " + buffer->Length.ToString() + " bytes of text were written to '" + file->Name + "':\n\n" + userContent;}catch(COMException^ ex){rootPage->HandleFileNotFoundException(ex);}});}else{OutputTextBlock->Text = "The text box is empty, please write something and then click 'Write' again.";}}
//写Byte流 using namespace Windows::Storage::Streams;rootPage->ResetScenarioOutput(OutputTextBlock);StorageFile^ file = rootPage->SampleFile;if (file != nullptr){String^ userContent = InputTextBox->Text;if (userContent != nullptr && !userContent->IsEmpty()){create_task(file->OpenTransactedWriteAsync()).then([this, file, userContent](task<StorageStreamTransaction^> task){try{StorageStreamTransaction^ transaction = task.get();DataWriter^ dataWriter = ref new DataWriter(transaction->Stream);dataWriter->WriteString(userContent);create_task(dataWriter->StoreAsync()).then([this, file, dataWriter, transaction, userContent](unsigned int bytesWritten){transaction->Stream->Size = bytesWritten; // reset stream size to override the filecreate_task(transaction->CommitAsync()).then([this, file, userContent](){OutputTextBlock->Text = "The following text was written to '" + file->Name + "' using a stream:\n\n" + userContent;});});}catch(COMException^ ex){rootPage->HandleFileNotFoundException(ex);}});}else{OutputTextBlock->Text = "The text box is empty, please write something and then click 'Write' again.";}}
//读byte流
rootPage->ResetScenarioOutput(OutputTextBlock);
StorageFile^ file = rootPage->SampleFile;
if (file != nullptr)
{
create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> task)
{
try
{
IBuffer^ buffer = task.get();
DataReader^ dataReader = DataReader::FromBuffer(buffer);
String^ fileContent = dataReader->ReadString(buffer->Length);
OutputTextBlock->Text = "The following " + buffer->Length.ToString() + " bytes of text were read from '" + file->Name + "':\n\n" + fileContent;
}
catch(COMException^ ex)
{
rootPage->HandleFileNotFoundException(ex);
}
});
}