逝水流年

This is a blog to record my life, my work, my feeling …

Win 8 文件读写操作

啥也不说了,直接上代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <windows.h>;
#include <ppltasks.h>;
using namespace Platform;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Concurrency;
StorageFile^ openFile(String^ fileName)
{
    StorageFile^ file = nullptr;
    StorageFolder^ rootFolder = Windows::Storage::ApplicationData::Current->;LocalFolder;
    task<StorageFile^>;(rootFolder->;GetFileAsync(fileName)).then([&]\(task<StorageFile^>; getFileTask\)
    {
        try
        {
            file = getFileTask.get();
        }
        catch (Platform::Exception^ e)
        {
        }
    }).wait();
    return file;
}
Array<byte>;^ getFileContent(String^ fileName)
{
    Array<byte>;^ data = nullptr;
    StorageFile^ file = openFile(fileName);
    if (file)
    {
        task<IRandomAccessStream^>;(file->;OpenAsync(FileAccessMode::Read)).then([&]
(task<IRandomAccessStream^>; getStream)
        {
            auto readStream = getStream.get();
            DataReader^ dataReader=ref new DataReader(readStream);
            int length = readStream->;Size;
            if (length >; 0)
            {
                data = ref new Array<byte>;(length);
                task<unsigned int>;(dataReader->;LoadAsync(readStream->;Size)).then([&]\(task<unsigned
int>; loadTask\) {
                    loadTask.get();
                    dataReader->;ReadBytes(data);
                }).wait();
            }
        }).wait();
    }
    return data;
}
bool setFileContent(String^ fileName, unsigned char* data, int size)
{
    size_t totalWritten = 0;
    bool complete = false;
    StorageFile^ file = openFile(fileName);
    if (file)
    {
        IRandomAccessStream^ readStream = nullptr;
        task<IRandomAccessStream^>;(file->;OpenAsync(FileAccessMode::ReadWrite)).then([&]
(task<IRandomAccessStream^>; getStream)
        {
            readStream = getStream.get();
            DataWriter^ dataWriter = ref new DataWriter(readStream);
            Array<unsigned char>;^ arr = ref new Array<unsigned char>;(data, size);
            dataWriter->;WriteBytes(arr);
            task<unsigned int>;(dataWriter->;StoreAsync()).then([&]\(task<unsigned int>; writeTask)
            {
                try
                {
                    totalWritten = writeTask.get();
                } catch (Platform::Exception^ ex) {}
            }).wait();
            task<bool>;(dataWriter->;FlushAsync()).then([&]\(task<bool>; flushTask)
            {
                try
                {
                    complete = flushTask.get();
                } catch (Platform::Exception^ ex) {}
            }).wait();
            dataWriter->;DetachStream();
        }).wait();
    }
    return complete;
}

Comments