CK.Core.FileUtil.WriteUniqueTimedFile C# (CSharp) Method

WriteUniqueTimedFile() public static method

Creates a new necessarily unique file and writes bytes content in a directory that must exist. The file name is based on a DateTime, with an eventual uniquifier if a file already exists with the same name.
public static WriteUniqueTimedFile ( string pathPrefix, string fileSuffix, System.DateTime time, byte content, bool withUTF8Bom, int maxTryBeforeGuid = 3 ) : string
pathPrefix string The path prefix. Must not be null. Must be a valid path and may ends with a prefix for the file name itself.
fileSuffix string Suffix for the file name. Must not be null. Typically an extension (like ".txt").
time System.DateTime The time that will be used to create the file name. It should be an UTC time.
content byte The bytes to write. Can be null or empty if the file must only be created.
withUTF8Bom bool True to write the UTF8 Byte Order Mask (the preamble).
maxTryBeforeGuid int Maximum value for short hexa uniquifier before using a base 64 guid suffix. Must between 0 and 15 (included).
return string
        public static string WriteUniqueTimedFile( string pathPrefix, string fileSuffix, DateTime time, byte[] content, bool withUTF8Bom, int maxTryBeforeGuid = 3 )
        {
            string fullLogFilePath;
            using( var f = CreateAndOpenUniqueTimedFile( pathPrefix, fileSuffix, time, FileAccess.Write, FileShare.Read, 8, FileOptions.SequentialScan | FileOptions.WriteThrough, maxTryBeforeGuid ) )
            {
                Debug.Assert( Encoding.UTF8.GetPreamble().Length == 3 );
                if( withUTF8Bom ) f.Write( Encoding.UTF8.GetPreamble(), 0, 3 );
                if( content != null && content.Length > 0 ) f.Write( content, 0, content.Length );
                fullLogFilePath = f.Name;
            }
            return fullLogFilePath;
        }

Usage Example

        static void HandleError(string s)
        {
            string    fullLogFilePath       = null;
            Exception errorWhileWritingFile = null;
            // Atomically captures the LogPath to use.
            string logPath = _logPath;

            if (logPath != null)
            {
                try
                {
                    fullLogFilePath = FileUtil.WriteUniqueTimedFile(logPath + SubDirectoryName, ".txt", DateTime.UtcNow, Encoding.UTF8.GetBytes(s), true);
                }
                catch (Exception ex)
                {
                    errorWhileWritingFile = ex;
                }
            }
            var h = OnError;

            if (h != null)
            {
                LowLevelErrorEventArgs e = new LowLevelErrorEventArgs(s, fullLogFilePath, errorWhileWritingFile);
                // h.GetInvocationList() creates an independent copy of Delegate[].
                foreach (EventHandler <LowLevelErrorEventArgs> d in h.GetInvocationList())
                {
                    try
                    {
                        d(null, e);
                    }
                    catch (Exception ex)
                    {
                        OnError -= (EventHandler <LowLevelErrorEventArgs>)d;
                        ActivityMonitor.CriticalErrorCollector.Add(ex, "While raising SystemActivityMonitor.OnError event.");
                    }
                }
            }
        }