win8学习--------File
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;
}
}
//另加空間名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);
}
});
}
轉載于:https://www.cnblogs.com/win-and-first/archive/2012/08/21/win8.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的win8学习--------File的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: crontab环境变量
- 下一篇: 小议 find