/// <summary> /// Writes the profile using the specified file provider /// </summary> /// <param name="provider">The file provider used to save the profile</param> public void Save(IFileProvider provider, bool serializeCache = true) { foreach (string env in AzureEnvironment.PublicEnvironments.Keys) { EnvironmentTable.Remove(env); } try { TryRemoveContext(Constants.DefaultValue); string contents = ToString(serializeCache); string diskContents = string.Empty; diskContents = provider.CreateReader().ReadToEnd(); if (diskContents != contents) { var writer = provider.CreateWriter(); writer.Write(contents); writer.Flush(); // When writing to a stream, ensure that the file is truncated // so that previous data is overwritten provider.Stream.SetLength(provider.Stream.Position); } } finally { // Adding back predefined environments foreach (AzureEnvironment env in AzureEnvironment.PublicEnvironments.Values) { EnvironmentTable[env.Name] = env; } } }