public void Flush() { try { if (fileStream != null && !this.IsClosed && isModified) { stream.Flush(); byte[] bytes = stream.ToArray(); fileStream.Seek(0); var streamTemp = fileStream.AsStream(); streamTemp.Write(bytes, 0, bytes.Length); streamTemp.Flush(); fileStream.FlushAsync().AsTask().Wait(); isModified = false; } } catch (FileNotFoundException ex)//did not found a better way to check if file exists { } }
public static StreamWriter AppendText(string path, Encoding encoding) { try { IAsyncOperation <StorageFile> fileAsync = FileHelper.GetFolderForPathOrURI(path).CreateFileAsync(Path.GetFileName(path), CreationCollisionOption.OpenIfExists); WindowsRuntimeSystemExtensions.AsTask <StorageFile>(fileAsync).Wait(); IAsyncOperation <IRandomAccessStream> source = fileAsync.GetResults().OpenAsync(FileAccessMode.ReadWrite); WindowsRuntimeSystemExtensions.AsTask <IRandomAccessStream>(source).Wait(); FileRandomAccessStream randomAccessStream = (FileRandomAccessStream)source.GetResults(); randomAccessStream.Seek(randomAccessStream.Size); return(encoding == null ? new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream)) : new StreamWriter(WindowsRuntimeStreamExtensions.AsStream((IRandomAccessStream)randomAccessStream), encoding)); } catch (Exception ex) { throw File.GetRethrowException(ex); } }