【问题标题】:Writing (updating) string to IsolatedStorage does not work将字符串写入(更新)到 IsolatedStorage 不起作用
【发布时间】:2013-01-22 02:20:32
【问题描述】:

当应用程序启动时,会执行一个文本文件的更新检查。如果远程文件的版本号高于本地版本,则将远程文件保存到隔离存储并替换当前文件。不幸的是,它没有按预期工作。旧文件还在存储中。

    public static bool WriteFileToIsolatedStorage(String content, String fileName)
    {
        bool result = false;

        MessageBox.Show(Leser.GetVersionInfo(content));

        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Truncate, store));

        try
        {
            writer.Write(content);
            writer.Flush();
            writer.Close();

            result = true;
        }
        catch
        {
            result = false;
        }

        return result;
    }

我尝试了几个 FileMode 参数,如“Create”或“Truncate”,但是当应用程序重新启动时,总​​是有可用的更新。写入存储时没有错误,但版本号表明它无法正常工作。

我的项目资源管理器中有一个文本文件,其中包含应按上述方式更新的数据。一开始我检查这个文件的存储,如果没有找到,它会被写入。这按预期工作。但是更新的版本没有写,我不知道为什么不写。

解决方案:写作工作正常 :) 问题是我读取了 ressource 文件,而不是保存到 IsolatedStorage 的文件

    public static String ReadFileContents(String file)
    {
        String content = String.Empty;

        var ResourceStream = Application.GetResourceStream(new Uri(file, UriKind.Relative));
        if (ResourceStream != null)
        {
            Stream myFileStream = ResourceStream.Stream;
            if (myFileStream.CanRead)
            {
                StreamReader myStreamReader = new StreamReader(myFileStream);
                content = myStreamReader.ReadToEnd();
            }
        }

        return content;
    }

对不起各位!

【问题讨论】:

    标签: c# windows-phone-7 isolatedstorage


    【解决方案1】:

    这是我用来创建文件并替换它的代码....只需更改关键字Truncate to Create 即可帮助您。

    public static bool WriteFileToIsolatedStorage(String content, String fileName)
        {
            bool result = false;
    
            //MessageBox.Show(Leser.GetVersionInfo(content));
    
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
    
            StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Create, store));
    
            try
            {
                writer.Write(content);
                writer.Flush();
                writer.Close();
    
                result = true;
            }
            catch
            {
                result = false;
            }
    
            StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, store));
            string rawData = reader.ReadToEnd();
            reader.Close();
    
    
            return result;
        }
    

    我使用流阅读器只是为了验证它是否被替换:)

    【讨论】:

    • 如上所述,我已经尝试了“Create”参数,并且我注意到错误在另一个函数中,但无论如何:)
    【解决方案2】:

    我看不出您的代码有任何明显错误,但试试这个:

        public static bool WriteFileToIsolatedStorage(String content, String fileName)
        {
            bool result = false;
    
            MessageBox.Show(Leser.GetVersionInfo(content));
            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(fileName, FileMode.Truncate, store))
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write(content);
    
                    result = true;
                }
            }
            catch
            {
                result = false;
            }
    
            return result;
        }
    

    处理你实例化的任何东西不仅是一个好主意,它还应该确保所有东西都被刷新和关闭(即使这可能在 StreamWriter 中隐式发生,所以这有点在黑暗中拍摄)

    【讨论】:

    • 没有帮助,但我将在未来使用 using 子句 thx ;)
    猜你喜欢
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多