diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs new file mode 100644 index 00000000..cc67e8da --- /dev/null +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/File/FileIO.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronIO; +using PepperDash.Core; + +namespace PepperDash.Essentials.Core +{ + public static class FileIO + { + + static CCriticalSection fileLock = new CCriticalSection(); + /// + /// + /// + /// + /// + public static bool FileExsists(string fileName) + { + if (GetFile(fileName) == null) + { + return false; + } + else + { + return true; + } + } + /// + /// Get the full file info from a path/filename, can include wildcards. + /// + /// + /// + public static FileInfo[] GetFiles(string fileName) + { + DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName)); + var files = dirInfo.GetFiles(Path.GetFileName(fileName)); + Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName); + if (files.Count() > 0) + { + return files; + } + else + { + return null; + } + } + + public static FileInfo GetFile(string fileName) + { + DirectoryInfo dirInfo = new DirectoryInfo(Path.GetDirectoryName(fileName)); + var files = dirInfo.GetFiles(Path.GetFileName(fileName)); + Debug.Console(0, "FileIO found: {0}, {1}", files.Count(), fileName); + if (files.Count() > 0) + { + return files.FirstOrDefault(); + } + else + { + return null; + } + } + + + /// + /// Get the data from a fileName + /// + /// + /// + public static string GetDataFromFile(FileInfo file) + { + try + { + DirectoryInfo dirInfo = new DirectoryInfo(file.Name); + Debug.Console(2, "FileIO Getting Data {0}", file.FullName); + + if (File.Exists(file.FullName)) + { + using (StreamReader r = new StreamReader(file.FullName)) + { + return r.ReadToEnd(); + } + } + else + { + Debug.Console(2, "File {0} Does not exsist", file.FullName); + return ""; + } + + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO read failed: \r{0}", e); + return ""; + } + } + + /// + /// + /// + /// + /// + public static void WriteDataToFile(string data, string filePath) + { + Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to write file: '{0}'", filePath); + + try + { + if (fileLock.TryEnter()) + { + using (StreamWriter sw = new StreamWriter(filePath)) + { + sw.Write(data); + sw.Flush(); + } + + } + else + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "FileIO Unable to enter FileLock"); + } + + } + catch (Exception e) + { + Debug.Console(0, Debug.ErrorLogLevel.Error, "Error: FileIO write failed: \r{0}", e); + } + finally + { + if (fileLock != null && !fileLock.Disposed) + fileLock.Leave(); + + } + } + + /// + /// + /// + /// + public static bool FileIoUnitTest() + { + var testData = "Testing FileIO"; + FileIO.WriteDataToFile(testData, "\\USER\\FileIOTest.pdt"); + + var file = FileIO.GetFile("\\USER\\*FileIOTest*"); + + var readData = FileIO.GetDataFromFile(file); + Debug.Console(0, "Returned {0}", readData); + File.Delete(file.FullName); + if (testData == readData) + { + return true; + } + else + { + return false; + } + } + + } +} \ No newline at end of file diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj index cdf6406c..7123a150 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/PepperDash_Essentials_Core.csproj @@ -224,6 +224,7 @@ +