1 //// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
2 //// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
3 //// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
4 //// PARTICULAR PURPOSE.
6 //// Copyright (c) Microsoft Corporation. All rights reserved
9 #include "BasicReaderWriter.h"
12 using namespace Microsoft::WRL;
13 using namespace Windows::Storage;
14 using namespace Windows::Storage::FileProperties;
15 using namespace Windows::Storage::Streams;
16 using namespace Windows::Foundation;
17 using namespace Windows::ApplicationModel;
18 using namespace Concurrency;
20 BasicReaderWriter::BasicReaderWriter()
22 m_location = Package::Current->InstalledLocation;
23 m_locationPath = Platform::String::Concat(m_location->Path, "\\");
26 BasicReaderWriter::BasicReaderWriter(
27 _In_ Windows::Storage::StorageFolder^ folder
31 Platform::String^ path = m_location->Path;
32 if (path->Length() == 0)
34 // Applications are not permitted to access certain
35 // folders, such as the Documents folder, using this
36 // code path. In such cases, the Path property for
37 // the folder will be an empty string.
38 throw ref new Platform::FailureException();
40 m_locationPath = Platform::String::Concat(path, "\\");
43 Platform::Array<byte>^ BasicReaderWriter::ReadData(
44 _In_ Platform::String^ filename
47 CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
48 extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
49 extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
50 extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
51 extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
52 extendedParams.lpSecurityAttributes = nullptr;
53 extendedParams.hTemplateFile = nullptr;
55 Wrappers::FileHandle file(
57 Platform::String::Concat(m_locationPath, filename)->Data(),
64 if (file.Get()==INVALID_HANDLE_VALUE)
66 throw ref new Platform::FailureException();
69 FILE_STANDARD_INFO fileInfo = {0};
70 if (!GetFileInformationByHandleEx(
77 throw ref new Platform::FailureException();
80 if (fileInfo.EndOfFile.HighPart != 0)
82 throw ref new Platform::OutOfMemoryException();
85 Platform::Array<byte>^ fileData = ref new Platform::Array<byte>(fileInfo.EndOfFile.LowPart);
95 throw ref new Platform::FailureException();
101 void BasicReaderWriter::ReadDataAsync(
102 _In_ Platform::String^ filename,
103 _In_ ReadDataAsyncCallback^ callback
106 task<StorageFile^>(m_location->GetFileAsync(filename)).then([=](StorageFile^ file)
108 return FileIO::ReadBufferAsync(file);
109 }).then([=](IBuffer^ buffer)
111 auto fileData = ref new Platform::Array<byte>(buffer->Length);
112 DataReader::FromBuffer(buffer)->ReadBytes(fileData);
113 callback(fileData, AsyncStatus::Completed);
117 uint32 BasicReaderWriter::WriteData(
118 _In_ Platform::String^ filename,
119 _In_ Platform::Array<byte>^ fileData
122 CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {0};
123 extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
124 extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
125 extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
126 extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
127 extendedParams.lpSecurityAttributes = nullptr;
128 extendedParams.hTemplateFile = nullptr;
130 Wrappers::FileHandle file(
132 Platform::String::Concat(m_locationPath, filename)->Data(),
139 if (file.Get()==INVALID_HANDLE_VALUE)
141 throw ref new Platform::FailureException();
144 DWORD numBytesWritten;
153 numBytesWritten != fileData->Length
156 throw ref new Platform::FailureException();
159 return numBytesWritten;
162 void BasicReaderWriter::WriteDataAsync(
163 _In_ Platform::String^ filename,
164 _In_ Platform::Array<byte>^ fileData,
165 _In_ WriteDataAsyncCallback^ callback
168 task<StorageFile^>(m_location->CreateFileAsync(filename, CreationCollisionOption::ReplaceExisting)).then([=](StorageFile^ file)
170 return FileIO::WriteBytesAsync(file, fileData);
173 callback(AsyncStatus::Completed);