Esempio n. 1
0
        /// <summary>
        /// Writes the text to the specified file
        /// </summary>
        /// <param name="text">The text to write</param>
        /// <param name="path">The path of the file to write to</param>
        /// <param name="append">If true, write the text to the end of the file, otherwise overrides any existing file</param>
        /// <returns></returns>
        public async Task WriteAllTextToFileAsync(string text, string path, bool append = false)
        {
            // TODO: Add exception catching

            // Normalize  path
            path = NormalizePath(path);

            // Resolve to absolute path
            path = ResolvePath(path);

            // Lock the task
            await AsyncAwaiter.AwaitAsync(nameof(FileManager) + path, async() =>
            {
                // Run the synchronous file access as a new task
                await CoreDI.Task.Run(() =>
                {
                    // Write the log message to file
                    using (var fileStream = (TextWriter) new StreamWriter(File.Open(path, append ? FileMode.Append : FileMode.Create, FileAccess.Write)))
                        fileStream.Write(text);
                });
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Writes the text to the specified file
        /// </summary>
        /// <param name="text">The text to write</param>
        /// <param name="path">The path of the file to write to</param>
        /// <param name="append">If true, append text to end, else overwrite</param>
        /// <returns></returns>
        public async Task WriteTextToFileAsync(string text, string path, bool append = false)
        {
            // ToDo: add exception catching

            //Normalize
            path = NormalizePath(path);

            //resolve to absolute path
            path = ResolvePath(path);

            //Lock the task
            await AsyncAwaiter.AwaitAsync(nameof(FileManager) + path, async() =>
            {
                //TODO: Add IoC.Task.Run that logs to logger on failure

                //Run the synchronous file as a new task
                await IoC.Task.Run(() =>
                {
                    //write log message to file
                    using (var fileStream = (TextWriter) new StreamWriter(File.Open(path, append ? FileMode.Append : FileMode.Create)))
                        fileStream.Write(text);
                });
            });
        }