Provides helper methods for reading and writing a file using the absolute path or URI of the file.
PlatformVersion supported AndroidAndroid 4.4 and later iOSiOS 9.0 and later tvOStvOS 9.0 and later TizenTizen 3.0 Windows UWPWindows 10 Windows StoreWindows 8.1 or later Windows Phone StoreWindows Phone 8.1 or later Windows Phone SilverlightWindows Phone 8.0 or later Windows (Desktop Apps)Windows Vista or later
Ejemplo n.º 1
0
        /// <summary>
        /// Reads the contents of the specified file using the specified character encoding and returns lines of text.
        /// </summary>
        /// <param name="file">The file to read.</param>
        /// <param name="encoding">The character encoding of the file.</param>
        /// <returns>When this method completes successfully, it returns the contents of the file as a list (type IVector) of lines of text.
        /// Each line of text in the list is represented by a String object.</returns>
        public static Task <IList <string> > ReadLinesAsync(IStorageFile file, UnicodeEncoding encoding)
        {
#if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN
            return(PathIO.ReadLinesAsync(file.Path, encoding));
#elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81
            return(Windows.Storage.FileIO.ReadLinesAsync((Windows.Storage.StorageFile)((StorageFile)file), (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask());
#elif WINDOWS_PHONE
            return(Task.Run <IList <string> >(async() =>
            {
                String line;
                List <String> lines = new List <String>();

                Stream s = await file.OpenStreamForReadAsync();
                using (StreamReader sr = new StreamReader(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)))
                    while ((line = sr.ReadLine()) != null)
                    {
                        lines.Add(line);
                    }

                return lines.ToArray();
            }));
#else
            throw new PlatformNotSupportedException();
#endif
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes text to the specified file using the specified character encoding.
        /// </summary>
        /// <param name="file">The file that the text is written to.</param>
        /// <param name="contents">The text to write.</param>
        /// <param name="encoding">The character encoding of the file.</param>
        /// <returns>No object or value is returned when this method completes.</returns>
        public static Task WriteTextAsync(IStorageFile file, string contents, UnicodeEncoding encoding)
        {
#if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN
            return(PathIO.WriteTextAsync(file.Path, contents, encoding));
#elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81
            return(Windows.Storage.FileIO.WriteTextAsync((Windows.Storage.StorageFile)((StorageFile)file), contents, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask());
#elif WINDOWS_PHONE
            return(Task.Run(async() =>
            {
                Stream s = await file.OpenStreamForWriteAsync();
                using (StreamWriter sw = new StreamWriter(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)))
                    sw.Write(contents);
            }));
#else
            throw new PlatformNotSupportedException();
#endif
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads the contents of the specified file using the specified character encoding and returns text.
        /// </summary>
        /// <param name="file">The file to read.</param>
        /// <param name="encoding">The character encoding of the file.</param>
        /// <returns>When this method completes successfully, it returns the contents of the file as a text string.</returns>
        public static Task <string> ReadTextAsync(IStorageFile file, UnicodeEncoding encoding)
        {
#if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN
            return(PathIO.ReadTextAsync(file.Path, encoding));
#elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81
            return(Windows.Storage.FileIO.ReadTextAsync((Windows.Storage.StorageFile)((StorageFile)file), (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask());
#elif WINDOWS_PHONE
            return(Task.Run <string>(async() =>
            {
                Stream s = await((StorageFile)file).OpenStreamForReadAsync();
                using (StreamReader sr = new StreamReader(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)))
                    return await sr.ReadToEndAsync();
            }));
#else
            throw new PlatformNotSupportedException();
#endif
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Writes lines of text to the specified file using the specified character encoding.
        /// </summary>
        /// <param name="file">The file that the lines are written to.</param>
        /// <param name="lines">The list of text strings to append as lines.</param>
        /// <param name="encoding">The character encoding of the file.</param>
        /// <returns>No object or value is returned when this method completes.</returns>
        public static Task WriteLinesAsync(IStorageFile file, IEnumerable <string> lines, UnicodeEncoding encoding)
        {
#if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN
            return(PathIO.WriteLinesAsync(file.Path, lines, encoding));
#elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81
            return(Windows.Storage.FileIO.WriteLinesAsync((Windows.Storage.StorageFile)((StorageFile)file), lines, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask());
#elif WINDOWS_PHONE
            return(Task.Run(async() =>
            {
                Stream s = await file.OpenStreamForWriteAsync();
                using (StreamWriter sw = new StreamWriter(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)))
                {
                    foreach (string line in lines)
                    {
                        sw.WriteLine(line);
                    }
                }
            }));
#else
            throw new PlatformNotSupportedException();
#endif
        }