How to properly read and write a file using Windows.Storage on Windows Phone 8

后端 未结 2 901
孤城傲影
孤城傲影 2020-12-04 00:16

I\'m needing to just simply write to a file and read from a file in Windows Phone 8 using the Windows.Storage APIs. This is relatively easy using the old IsolatedStorage met

2条回答
  •  一生所求
    2020-12-04 00:32

    I haven't tried that with Windows Phone 8, but here's what WinRT XAML Toolkit has for Windows 8 that could work.

    using System;
    using System.Threading.Tasks;
    using Windows.Storage;
    using Windows.Storage.Streams;
    
    namespace WinRTXamlToolkit.IO.Extensions
    {
        /// 
        /// Extensions for simple writing and reading of strings to/from files.
        /// 
        /// 
        /// Note that these were created before FileIO class existed in WinRT, but they still serve a purpose.
        /// 
        public static class StringIOExtensions
        {
            /// 
            /// Reads a string from a text file.
            /// 
            /// Name of the file.
            /// The folder.
            /// 
            public static async Task ReadFromFile(
                string fileName,
                StorageFolder folder = null)
            {
                folder = folder ?? ApplicationData.Current.LocalFolder;
                var file = await folder.GetFileAsync(fileName);
    
                using (var fs = await file.OpenAsync(FileAccessMode.Read))
                {
                    using (var inStream = fs.GetInputStreamAt(0))
                    {
                        using (var reader = new DataReader(inStream))
                        {
                            await reader.LoadAsync((uint)fs.Size);
                            string data = reader.ReadString((uint)fs.Size);
                            reader.DetachStream();
    
                            return data;
                        }
                    }
                }
            }
    
            /// 
            /// Writes a string to a text file.
            /// 
            /// The text to write.
            /// Name of the file.
            /// The folder.
            /// 
            /// The enum value that determines how responds if the fileName is the same
            /// as the name of an existing file in the current folder. Defaults to ReplaceExisting.
            /// 
            /// 
            public static async Task WriteToFile(
                this string text,
                string fileName,
                StorageFolder folder = null,
                CreationCollisionOption options = CreationCollisionOption.ReplaceExisting)
            {
                folder = folder ?? ApplicationData.Current.LocalFolder;
                var file = await folder.CreateFileAsync(
                    fileName,
                    options);
                using (var fs = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (var outStream = fs.GetOutputStreamAt(0))
                    {
                        using (var dataWriter = new DataWriter(outStream))
                        {
                            if (text != null)
                                dataWriter.WriteString(text);
    
                            await dataWriter.StoreAsync();
                            dataWriter.DetachStream();
                        }
    
                        await outStream.FlushAsync();
                    }
                }
            }
        }
    }
    

提交回复
热议问题