博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
win8学习--------File
阅读量:5774 次
发布时间:2019-06-18

本文共 4845 字,大约阅读时间需要 16 分钟。

//创建文件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
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
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
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 file create_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

你可能感兴趣的文章
(step6.1.5)hdu 1233(还是畅通工程——最小生成树)
查看>>
Membership三步曲之进阶篇 - 深入剖析Provider Model
查看>>
huffman编码——原理与实现
查看>>
Linux移植随笔:终于解决Tslib的问题了【转】
查看>>
MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)
查看>>
【leetcode】Longest Common Prefix
查看>>
前端优化及相关要点总结
查看>>
Vue 列表渲染
查看>>
struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
查看>>
25 个精美的手机网站模板
查看>>
C#反射实例应用--------获取程序集信息和通过类名创建类实例
查看>>
VC中实现文字竖排的简单方法
查看>>
会话标识未更新
查看>>
【设计模式】数据访问对象模式
查看>>
Tomcat8 配置Oracle11g数据源
查看>>
【PHP面向对象(OOP)编程入门教程】8.构造方法__construct()与析构方法__destruct()
查看>>
ThinkPHP常用配置路径
查看>>
Java基础(二)面向对象(上)
查看>>
C# 操作的时候接收用户输入密码进行确认
查看>>
thrift 开发教程 - nick的日志 - 网易博客
查看>>