From b12e3df09eb2e8535906c4be2365a5dcd8d9c640 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Wed, 14 Oct 2015 14:30:31 -0400 Subject: [PATCH] That was easy - moved TemporaryFileManager from engine-specific WindowsForms.UserInterface to engine-agnostic UserInterface library --- ...alEditor.UserInterface.WindowsForms.csproj | 1 - .../TemporaryFileManager.cs | 74 +++++++++++++++++++ .../UniversalEditor.UserInterface.csproj | 1 + 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 CSharp/Libraries/UniversalEditor.UserInterface/TemporaryFileManager.cs diff --git a/CSharp/Engines/WindowsForms/Libraries/UniversalEditor.UserInterface.WindowsForms/UniversalEditor.UserInterface.WindowsForms.csproj b/CSharp/Engines/WindowsForms/Libraries/UniversalEditor.UserInterface.WindowsForms/UniversalEditor.UserInterface.WindowsForms.csproj index b15084a3..efb0afc5 100644 --- a/CSharp/Engines/WindowsForms/Libraries/UniversalEditor.UserInterface.WindowsForms/UniversalEditor.UserInterface.WindowsForms.csproj +++ b/CSharp/Engines/WindowsForms/Libraries/UniversalEditor.UserInterface.WindowsForms/UniversalEditor.UserInterface.WindowsForms.csproj @@ -149,7 +149,6 @@ True Resources.resx - diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/TemporaryFileManager.cs b/CSharp/Libraries/UniversalEditor.UserInterface/TemporaryFileManager.cs new file mode 100644 index 00000000..d100b4da --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.UserInterface/TemporaryFileManager.cs @@ -0,0 +1,74 @@ +using System; + +namespace UniversalEditor.UserInterface +{ + public class TemporaryFileManager + { + private static Random rand = new Random(); + + private static System.Collections.Specialized.StringCollection mvarTemporaryFileNames = new System.Collections.Specialized.StringCollection(); + private static string mvarTemporaryFilePath = null; + + public static string CreateTemporaryFile(string FileName, byte[] FileData = null) + { + if (mvarTemporaryFilePath == null) throw new InvalidOperationException(); + if (!System.IO.Directory.Exists(mvarTemporaryFilePath)) + { + System.IO.Directory.CreateDirectory(mvarTemporaryFilePath); + } + + FileName = System.IO.Path.GetFileName(FileName); + + string filePath = mvarTemporaryFilePath + System.IO.Path.DirectorySeparatorChar.ToString() + FileName; + if (FileData != null) System.IO.File.WriteAllBytes(filePath, FileData); + mvarTemporaryFileNames.Add(filePath); + return filePath; + } + + public static bool RegisterTemporaryDirectory(string prefix, int maxNameLength) + { + if (mvarTemporaryFilePath != null) + { + return false; + } + string name = String.Empty; + do + { + name = prefix + rand.Next(0, (maxNameLength * 10) - 1).ToString().PadLeft(maxNameLength - prefix.Length, '0'); + } + while (System.IO.Directory.Exists(name)); + + string pathName = System.IO.Path.GetTempPath() + System.IO.Path.DirectorySeparatorChar.ToString() + name; + System.IO.Directory.CreateDirectory(pathName); + + mvarTemporaryFilePath = pathName; + return true; + } + public static bool UnregisterTemporaryDirectory() + { + if (mvarTemporaryFilePath == null) return false; + + System.Collections.Generic.List fileNamesNotDeleted = new System.Collections.Generic.List(); + + foreach (string fileName in mvarTemporaryFileNames) + { + if (System.IO.File.Exists(fileName)) + { + try + { + System.IO.File.Delete(fileName); + } + catch (Exception ex) + { + fileNamesNotDeleted.Add(fileName); + } + } + } + if (System.IO.Directory.Exists(mvarTemporaryFilePath) && fileNamesNotDeleted.Count == 0) + { + System.IO.Directory.Delete(mvarTemporaryFilePath, true); + } + return true; + } + } +} \ No newline at end of file diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj b/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj index ae19623b..fdc4ab3e 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj +++ b/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj @@ -80,6 +80,7 @@ +