99
1010using System . Collections . Generic ;
1111using System . IO ;
12+ using System . Threading ;
1213using System . Threading . Tasks ;
1314
1415namespace PCLExt . FileStorage . Extensions
@@ -34,10 +35,11 @@ public static string ReadAllText(this IFile file)
3435 /// Reads the contents of a file as a string
3536 /// </summary>
3637 /// <param name="file">The file to read </param>
38+ /// <param name="cancellationToken">The cancellation token.</param>
3739 /// <returns>The contents of the file</returns>
38- public static async Task < string > ReadAllTextAsync ( this IFile file )
40+ public static async Task < string > ReadAllTextAsync ( this IFile file , CancellationToken cancellationToken = default ( CancellationToken ) )
3941 {
40- using ( var stream = await file . OpenAsync ( FileAccess . Read ) . ConfigureAwait ( false ) )
42+ using ( var stream = await file . OpenAsync ( FileAccess . Read , cancellationToken ) . ConfigureAwait ( false ) )
4143 using ( var sr = new StreamReader ( stream ) )
4244 return await sr . ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
4345 }
@@ -63,10 +65,11 @@ public static void WriteAllText(this IFile file, string contents)
6365 /// </summary>
6466 /// <param name="file">The file to write to</param>
6567 /// <param name="contents">The content to write to the file</param>
68+ /// <param name="cancellationToken">The cancellation token.</param>
6669 /// <returns>A task which completes when the write operation finishes</returns>
67- public static async Task WriteAllTextAsync ( this IFile file , string contents )
70+ public static async Task WriteAllTextAsync ( this IFile file , string contents , CancellationToken cancellationToken = default ( CancellationToken ) )
6871 {
69- using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite ) . ConfigureAwait ( false ) )
72+ using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite , cancellationToken ) . ConfigureAwait ( false ) )
7073 {
7174 stream . SetLength ( 0 ) ;
7275 using ( var sw = new StreamWriter ( stream ) )
@@ -96,10 +99,11 @@ public static string[] ReadAllLines(this IFile file)
9699 ///
97100 /// </summary>
98101 /// <param name="file"></param>
102+ /// <param name="cancellationToken">The cancellation token.</param>
99103 /// <returns></returns>
100- public static async Task < string [ ] > ReadAllLinesAsync ( this IFile file )
104+ public static async Task < string [ ] > ReadAllLinesAsync ( this IFile file , CancellationToken cancellationToken = default ( CancellationToken ) )
101105 {
102- using ( var stream = await file . OpenAsync ( FileAccess . Read ) . ConfigureAwait ( false ) )
106+ using ( var stream = await file . OpenAsync ( FileAccess . Read , cancellationToken ) . ConfigureAwait ( false ) )
103107 using ( var sr = new StreamReader ( stream ) )
104108 {
105109 var lines = new List < string > ( ) ;
@@ -130,9 +134,10 @@ public static void WriteAllLines(this IFile file, IEnumerable<string> lines)
130134 /// </summary>
131135 /// <param name="file"></param>
132136 /// <param name="lines"></param>
133- public static async Task WriteAllLinesAsync ( this IFile file , IEnumerable < string > lines )
137+ /// <param name="cancellationToken">The cancellation token.</param>
138+ public static async Task WriteAllLinesAsync ( this IFile file , IEnumerable < string > lines , CancellationToken cancellationToken = default ( CancellationToken ) )
134139 {
135- using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite ) . ConfigureAwait ( false ) )
140+ using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite , cancellationToken ) . ConfigureAwait ( false ) )
136141 {
137142 stream . SetLength ( 0 ) ;
138143 using ( var sw = new StreamWriter ( stream ) )
@@ -162,10 +167,11 @@ public static void AppendText(this IFile file, string contents)
162167 /// </summary>
163168 /// <param name="file"></param>
164169 /// <param name="contents"></param>
170+ /// <param name="cancellationToken">The cancellation token.</param>
165171 /// <returns></returns>
166- public static async Task AppendTextAsync ( this IFile file , string contents )
172+ public static async Task AppendTextAsync ( this IFile file , string contents , CancellationToken cancellationToken = default ( CancellationToken ) )
167173 {
168- using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite ) . ConfigureAwait ( false ) )
174+ using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite , cancellationToken ) . ConfigureAwait ( false ) )
169175 {
170176 stream . Seek ( stream . Length , SeekOrigin . Begin ) ;
171177 using ( var sw = new StreamWriter ( stream ) )
@@ -194,10 +200,11 @@ public static void AppendLines(this IFile file, IEnumerable<string> lines)
194200 /// </summary>
195201 /// <param name="file"></param>
196202 /// <param name="lines"></param>
203+ /// <param name="cancellationToken">The cancellation token.</param>
197204 /// <returns></returns>
198- public static async Task AppendLinesAsync ( this IFile file , IEnumerable < string > lines )
205+ public static async Task AppendLinesAsync ( this IFile file , IEnumerable < string > lines , CancellationToken cancellationToken = default ( CancellationToken ) )
199206 {
200- using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite ) . ConfigureAwait ( false ) )
207+ using ( var stream = await file . OpenAsync ( FileAccess . ReadAndWrite , cancellationToken ) . ConfigureAwait ( false ) )
201208 {
202209 stream . Seek ( stream . Length , SeekOrigin . Begin ) ;
203210 using ( var sw = new StreamWriter ( stream ) )
0 commit comments