0

我在 .txt 中有 17 个文件,我想将所有这些文件导入到 isolatedStorageDevice。

我怎样才能做到这一点???

记住:我不想写一个文件,我想把一个现有的文件放在那里。

文件位于 Project 的文件夹中,例如:(/Files/user.txt)

4

1 回答 1

0

手动

使用任何现有的 Windows Phone 独立存储资源管理器工具

以编程方式

您必须编写文件副本。假设有一个 CordovaSourceDictionary.xml 作为您的项目的一部分,它指定必须将哪些文件移动到 IsolatedStorage

<CordovaSourceDictionary>
    <FilePath Value="www\img\logo.png"/>
    <FilePath Value="www\js\index.js"/>
    <FilePath Value="www\cordova-2.1.0.js"/>
    <FilePath Value="www\css\index.css"/>
    <FilePath Value="www\index.html"/>
</CordovaSourceDictionary>

然后你可以使用下面的代码复制你的文件

StreamResourceInfo streamInfo = Application.GetResourceStream(new Uri("CordovaSourceDictionary.xml", UriKind.Relative));

if (streamInfo != null)
{
    StreamReader sr = new StreamReader(streamInfo.Stream);
    //This will Read Keys Collection for the xml file

    XDocument document = XDocument.Parse(sr.ReadToEnd());

    var files = from results in document.Descendants("FilePath")
                select new
                {
                    path = (string)results.Attribute("Value")
                };
    StreamResourceInfo fileResourceStreamInfo;

    using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {

        foreach (var file in files)
        {
            fileResourceStreamInfo = Application.GetResourceStream(new Uri(file.path, UriKind.Relative));

            if (fileResourceStreamInfo != null)
            {
                using (BinaryReader br = new BinaryReader(fileResourceStreamInfo.Stream))
                {
                    byte[] data = br.ReadBytes((int)fileResourceStreamInfo.Stream.Length);

                    string strBaseDir = AppRoot + file.path.Substring(0, file.path.LastIndexOf(System.IO.Path.DirectorySeparatorChar));

                    if (!appStorage.DirectoryExists(strBaseDir))
                    {
                        Debug.WriteLine("INFO: Creating Directory :: " + strBaseDir);
                        appStorage.CreateDirectory(strBaseDir);
                    }

                    // This will truncate/overwrite an existing file, or
                    using (IsolatedStorageFileStream outFile = appStorage.OpenFile(AppRoot + file.path, FileMode.Create))
                    {
                        Debug.WriteLine("INFO: Writing data for " + AppRoot + file.path + " and length = " + data.Length);
                        using (var writer = new BinaryWriter(outFile))
                        {
                            writer.Write(data);
                        }
                    }
                }
            }
            else
            {
                Debug.WriteLine("ERROR: Failed to write file :: " + file.path + " did you forget to add it to the project?");
            }
        }
    }
}
于 2012-11-28T16:10:11.567 回答