/// <summary> /// Reads the contents of the file at the specified path or URI using the specified character encoding and returns lines of text. /// </summary> /// <param name="absolutePath">The path of 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(string absolutePath, UnicodeEncoding encoding) { #if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(Task.Run <IList <string> >(() => { return File.ReadAllLines(absolutePath, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)); })); #elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81 return(Windows.Storage.PathIO.ReadLinesAsync(absolutePath, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask()); #elif WINDOWS_PHONE return(Task.Run <IList <string> >(() => { String line; List <String> lines = new List <String>(); Stream s = File.OpenRead(absolutePath); using (StreamReader sr = new StreamReader(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding))) while ((line = sr.ReadLine()) != null) { lines.Add(line); } return lines; })); #else throw new PlatformNotSupportedException(); #endif }
/// <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 }
/// <summary> /// Reads the contents of the file at the specified path or URI using the specified character encoding and returns text. /// </summary> /// <param name="absolutePath">The path of 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(string absolutePath, UnicodeEncoding encoding) { #if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(Task.Run <string>(() => { return File.ReadAllText(absolutePath, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)); })); #elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81 return(Windows.Storage.PathIO.ReadTextAsync(absolutePath, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask()); #elif WINDOWS_PHONE Stream s = File.OpenRead(absolutePath); using (StreamReader sr = new StreamReader(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding))) return(sr.ReadToEndAsync()); #else throw new PlatformNotSupportedException(); #endif }
/// <summary> /// Writes text to the file at the specified path or URI using the specified character encoding. /// </summary> /// <param name="absolutePath">The path of 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(string absolutePath, string contents, UnicodeEncoding encoding) { #if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(Task.Run(() => { File.WriteAllText(absolutePath, contents, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)); })); #elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81 return(Windows.Storage.PathIO.WriteTextAsync(absolutePath, contents, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask()); #elif WINDOWS_PHONE return(Task.Run(() => { Stream s = File.OpenWrite(absolutePath); using (StreamWriter sw = new StreamWriter(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding))) sw.Write(contents); })); #else throw new PlatformNotSupportedException(); #endif }
/// <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 }
/// <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 }
/// <summary> /// Writes lines of text to the file at the specified path or URI using the specified character encoding. /// </summary> /// <param name="absolutePath">The path of 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(string absolutePath, IEnumerable <string> lines, UnicodeEncoding encoding) { #if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(Task.Run(() => { File.WriteAllLines(absolutePath, lines, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding)); })); #elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81 return(Windows.Storage.PathIO.WriteLinesAsync(absolutePath, lines, (Windows.Storage.Streams.UnicodeEncoding)((int)encoding)).AsTask()); #elif WINDOWS_PHONE return(Task.Run(() => { Stream s = File.OpenWrite(absolutePath); using (StreamWriter sw = new StreamWriter(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding))) { foreach (string line in lines) { sw.WriteLine(line); } } })); #else throw new PlatformNotSupportedException(); #endif }
/// <summary> /// Appends lines of text to the specified file using the specified character encoding. /// </summary> /// <param name="file">The file that the lines are appended 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 AppendLinesAsync(IStorageFile file, IEnumerable <string> lines, UnicodeEncoding encoding) { #if __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN return(PathIO.AppendLinesAsync(file.Path, lines, encoding)); #elif WINDOWS_UWP || WINDOWS_APP || WINDOWS_PHONE_APP || WINDOWS_PHONE_81 return(Windows.Storage.FileIO.AppendLinesAsync((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(); s.Position = s.Length; using (StreamWriter sw = new StreamWriter(s, UnicodeEncodingHelper.EncodingFromUnicodeEncoding(encoding))) { foreach (string line in lines) { sw.WriteLine(line); } } })); #else throw new PlatformNotSupportedException(); #endif }