Ejemplo n.º 1
0
        public static void WriteIfNeeded(string filePath, string existingContents, string newContents)
        {
            if (string.CompareOrdinal(newContents.Trim(), existingContents.Trim()) == 0)
            {
                return;
            }

            using (var fileStream = ExclusiveFileStream.Acquire(
                       filePath,
                       FileMode.OpenOrCreate,
                       FileShare.Read,
                       FileAccess.ReadWrite))
            {
                WriteContents(fileStream.Value, newContents);
            }
        }
Ejemplo n.º 2
0
        public static string LoadContents(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return(string.Empty);
            }

            using (var fileStream = ExclusiveFileStream.Acquire(
                       filePath,
                       FileMode.Open,
                       FileShare.Read,
                       FileAccess.Read))
            {
                fileStream.Value.Seek(0, SeekOrigin.Begin);
                using (var reader = CreateStreamReader(fileStream.Value))
                {
                    return(reader.ReadToEnd());
                }
            }
        }