无法从WP7中的独立存储中读取文件

时间:2012-06-20 12:17:48

标签: c# windows-phone-7

我的代码如下所示:

private void LayoutRoot_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        // TODO: Add event handler implementation here.
        try
        {
            string fileName = NavigationContext.QueryString["id"];
            DataSaver<SampleData> mypersonaldata = new DataSaver<SampleData>();
            var PersonData = mypersonaldata.LoadMyData(fileName);

            txtName.Text = PersonData.Name;
            txtAge.Text = PersonData.Age;
            txtheight.Text = PersonData.Height;
            txtMammals.Text = PersonData.FavMammals;
            txtBirds.Text = PersonData.FavBirds;
            txtReptiles.Text = PersonData.FavReptiles;
        }
        catch
        {
            MessageBox.Show("Unable to read!");
        }
    }

以下是我加载数据的代码:

public MyDataType LoadMyData(string sourceName)
{
    MyDataType retVal = default(MyDataType);
    string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, sourceName);
    if (IsoFile.FileExists(TargetFileName))
        using (var sourceStream = IsoFile.OpenFile(TargetFileName, FileMode.Open))
        {
            retVal = (MyDataType)_mySerializer.ReadObject(sourceStream);
        }
    return retVal;
}

我可以访问DataSaver.cs和SampleData.cs的成员。我也得到了正确的文件名但无法读取数据。请帮忙!

    public void SaveMyData(MyDataType sourceData) 
    { 
        string TargetFileName = String.Format("{0}/{1}.dat", TargetFolderName, targetFileName); 

        if (!IsoFile.DirectoryExists(TargetFolderName)) 
            IsoFile.CreateDirectory(TargetFolderName); 

        try 
        { 
            using (var targetFile = IsoFile.CreateFile(TargetFileName)) 
            { 
                _mySerializer.WriteObject(targetFile, sourceData); 
            } 
        } 
        catch (Exception e)
        {
            IsoFile.DeleteFile(TargetFileName); 
        }
    }    

以上代码用于将数据保存到隔离存储中。

1 个答案:

答案 0 :(得分:1)

这两种静态方法应该可以帮助您进行WP隔离存储的任何读/写操作。

public static void WriteStreamToFile(MemoryStream stream, string baseFileName, string extension, out string realFileName)
    {
        realFileName = String.Empty;

        if (stream != null)
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                baseFileName = GlobalSettings.NormalizeFileName(baseFileName);

                realFileName = String.Concat(baseFileName, extension);

                using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(realFileName, FileMode.Create, FileAccess.Write))
                    stream.WriteTo(fileStream);

            }
    }

    public static void ReadStreamFromFile(MemoryStream stream, string fileName)
    {
        if (stream != null)
            using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
            using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
                fileStream.CopyTo(stream);
    }

然后,您可以序列化您想要的所有内容:

public static object DeserializeObject(string fileName, Type objectType)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read))
        using (TextReader xmlReader = new StreamReader(fileStream))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectType);

            return xmlSerializer.Deserialize(xmlReader);
        }
    }

    public static void SerializeObject(string fileName, object target, Type objectType)
    {
        using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
        using (IsolatedStorageFileStream fileStream = appStorage.OpenFile(fileName, FileMode.Create, FileAccess.Write))
        using (TextWriter xmlWriter = new StreamWriter(fileStream))
        {
            XmlSerializer xmlSerializer = new XmlSerializer(objectType);

            xmlSerializer.Serialize(xmlWriter, target);
        }
    }