-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated teh IO modules to allow for async operations
- Loading branch information
Showing
8 changed files
with
188 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Text; | ||
|
||
namespace Configuration | ||
{ | ||
internal static class IO | ||
{ | ||
internal static string ReadFileText(string filePath) | ||
{ | ||
try | ||
{ | ||
if (!File.Exists(filePath)) return ""; | ||
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
using (var sr = new StreamReader(fs)) | ||
{ | ||
return sr.ReadToEnd(); | ||
} | ||
} | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
|
||
internal static async Task SaveFile(string filePath, string source) | ||
{ | ||
try | ||
{ | ||
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) | ||
{ | ||
using (var sr = new StreamWriter(fs)) | ||
{ | ||
await sr.WriteAsync(source); | ||
sr.Flush(); | ||
sr.Close(); | ||
sr.Dispose(); | ||
} | ||
fs.Close(); | ||
fs.Dispose(); | ||
} | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
|
||
internal static async Task SaveFile(string fileName, string directoryName, string source) | ||
{ | ||
try | ||
{ | ||
var filePath = System.IO.Path.GetFullPath(fileName); | ||
Directory.CreateDirectory(directoryName); | ||
await SaveFile(filePath, source); | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Text; | ||
|
||
namespace Project | ||
{ | ||
internal static class IO | ||
{ | ||
internal static string ReadFileText(string filePath) | ||
{ | ||
try | ||
{ | ||
if (!File.Exists(filePath)) return ""; | ||
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) | ||
{ | ||
using (var sr = new StreamReader(fs)) | ||
{ | ||
return sr.ReadToEnd(); | ||
} | ||
} | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
|
||
internal static async Task SaveFile(string filePath, string source) | ||
{ | ||
try | ||
{ | ||
using (var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) | ||
{ | ||
using (var sr = new StreamWriter(fs)) | ||
{ | ||
await sr.WriteAsync(source); | ||
sr.Flush(); | ||
sr.Close(); | ||
sr.Dispose(); | ||
} | ||
fs.Close(); | ||
fs.Dispose(); | ||
} | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
|
||
internal static async Task SaveFile(string fileName, string directoryName, string source) | ||
{ | ||
try | ||
{ | ||
Directory.CreateDirectory(directoryName); | ||
await SaveFile(fileName, source); | ||
} | ||
catch (IOException ioe) | ||
{ | ||
Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); | ||
throw ioe; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters