wp8使用fopen或来自c ++的CreateFile访问独立存储 [英] wp8 access isolated storage with fopen or CreateFile from c++

查看:97
本文介绍了wp8使用fopen或来自c ++的CreateFile访问独立存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试访问复制到WP8仿真器设备的独立存储的文件。使用fopen我只能访问本地安装文件夹。

I'm trying to access files copied to the isolated storage of an WP8 emulator device. With fopen I've only access to the local installation folder.

有没有办法直接从c ++访问isostore文件操作?

Is there a way to access the isostore for file operations directly from c++?

谢谢,

Thanks,

Jürgen

推荐答案

您好Jürgen,



您可以在"适用于Windows Phone 8的Marble Maze示例"中找到C ++ / wp8的隔离存储示例。 (http://code.msdn.microsoft.com/wpapps/Marble-Maze-sample-for-c9f3706b)。
$
在MarbleMaze.cpp中,您有2个具有隔离存储功能:

void MarbleMaze :: SaveState(Windows :: ApplicationModel :: SuspendingDeferral ^ deferral)

void MarbleMaze :: LoadState()



这是将一个int和一个字符串写入隔离存储文件的示例。 int是保存字符串的长度。

Hi Jürgen,

You can find an example of isolate storage for C++/wp8 in "Marble Maze sample for Windows Phone 8" (http://code.msdn.microsoft.com/wpapps/Marble-Maze-sample-for-c9f3706b).
In MarbleMaze.cpp, you have 2 functions with isolate storage:
void MarbleMaze::SaveState(Windows::ApplicationModel::SuspendingDeferral^ deferral)
void MarbleMaze::LoadState()

This is an example to write an int and a string to an isolate storage file. The int is to save the length of the string.

#include< ppltasks.h>

使用命名空间Windows :: Storage;

using namespace Concurrency;

using namespace Platform;

#include <ppltasks.h>
using namespace Windows::Storage;
using namespace Concurrency;
using namespace Platform;

void FileManager :: WriteFile(String ^ strFile,String ^ strContent)

{

     auto folder = ApplicationData :: Current-> LocalFolder;

   &NBSP;任务< StorageFile ^> getFileTask(folder-> CreateFileAsync(strFile,CreationCollisionOption :: ReplaceExisting));
$


     //创建一个本地以允许在lambdas之间传递DataReader。

     auto writer = std :: make_shared< Streams :: DataWriter ^>(nullptr);



     getFileTask.then([](StorageFile ^ file)

    {

       return file - > OpenAsync(FileAccessMode :: ReadWrite);

   })。then([this,writer,strContent](Streams :: IRandomAccessStream ^ stream)

    {

       Streams :: DataWriter ^ state = ref new Streams :: DataWriter(stream);

       * writer = state;



      无符号整型CODEUNITS =国有> MeasureString(strContent);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;国有> WriteUInt32(CODEUNITS);

       state-> WriteString(strContent);



       return state-> StoreAsync();

   })。then([writer](uint32 count)

    {

     & nbsp; return(* writer) - > FlushAsync();

    })。then([this,writer](bool flushed)

    {

       ;删除(*作家);

   });

}

$
无效FileManager :: LoadFile(String ^ strFile)

{

     auto folder = ApplicationData :: Current-> LocalFolder;

   &NBSP;任务< StorageFile ^> getFileTask(folder-> GetFileAsync(strFile));
$


     //创建一个本地以允许在lambdas之间传递DataReader。

     auto reader = std :: make_shared< Streams :: DataReader ^>(nullptr);

     getFileTask.then([this,reader](任务< StorageFile ^> fileTask)

    {

     ;  尝试
       {

         ;  StorageFile ^ file = fileTask.get();
$


          task< Streams :: IRandomAccessStreamWithContentType ^>(文件 - > OpenReadAsync()),然后([读者](流:: IRandomAccessStreamWithContentType ^流)

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;   {

             * reader = ref new Streams :: DataReader(stream) ;

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;返回(*读者) - > LoadAsync(的static_cast< UINT32>(于流>大小));

         })。then([this,reader](uint32 b ytesRead)

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   {



   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   Streams :: DataReader ^ state =(* reader);

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  试试
   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   {

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   unsigned int codeUnits = state-> ReadUInt32();

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   Platform :: String ^ strContent = state-> ReadString(codeUnits);



   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  }

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   catch(平台::例外^ e)

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   {

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   //什么也不做。

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  }

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;  });;
$
   &NBSP;&NBSP;&NBSP;  }

   &NBSP;&NBSP;&NBSP;   catch(平台::例外^ e)

   &NBSP;&NBSP;&NBSP;   {



   &NBSP;&NBSP;&NBSP;  }

    });

}

void FileManager::WriteFile( String^ strFile, String^ strContent )
{
    auto folder = ApplicationData::Current->LocalFolder;
    task<StorageFile^> getFileTask(folder->CreateFileAsync( strFile, CreationCollisionOption::ReplaceExisting));

    // Create a local to allow the DataReader to be passed between lambdas.
    auto writer = std::make_shared<Streams::DataWriter^>(nullptr);

    getFileTask.then([](StorageFile^ file)
    {
        return file->OpenAsync(FileAccessMode::ReadWrite);
    }).then([this, writer, strContent](Streams::IRandomAccessStream^ stream)
    {
        Streams::DataWriter^ state = ref new Streams::DataWriter(stream);
        *writer = state;

        unsigned int codeUnits = state->MeasureString(strContent);
        state->WriteUInt32(codeUnits);
        state->WriteString(strContent);

        return state->StoreAsync();
    }).then([writer](uint32 count)
    {
        return (*writer)->FlushAsync();
    }).then([this, writer](bool flushed)
    {
        delete (*writer);
    });
}

void FileManager::LoadFile(String^ strFile)
{
    auto folder = ApplicationData::Current->LocalFolder;
    task<StorageFile^> getFileTask(folder->GetFileAsync(strFile));

    // Create a local to allow the DataReader to be passed between lambdas.
    auto reader = std::make_shared<Streams::DataReader^>(nullptr);
    getFileTask.then([this, reader](task<StorageFile^> fileTask)
    {
        try
        {
            StorageFile^ file = fileTask.get();

            task<Streams::IRandomAccessStreamWithContentType^> (file->OpenReadAsync()).then([reader](Streams::IRandomAccessStreamWithContentType^ stream)
            {
                *reader = ref new Streams::DataReader(stream);
                return (*reader)->LoadAsync(static_cast<uint32>(stream->Size));
            }).then([this, reader](uint32 bytesRead)
            {

                Streams::DataReader^ state = (*reader);
                try
                {
                        unsigned int codeUnits = state->ReadUInt32();
                        Platform::String^ strContent = state->ReadString(codeUnits);

                }
                catch (Platform::Exception^ e)
                {
                    // Do nothing.
                }
            });;
        }
        catch (Platform::Exception^ e)
        {

        }
    });
}

问候,

AlanLive


这篇关于wp8使用fopen或来自c ++的CreateFile访问独立存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆