public static void WritePSDataCollectionProfile(string product, PSDataCollectionProfile profile) { try { var store = new DiskDataStore(); string dataPath = Path.Combine(SSPowerShell.ProfileDirectory, product, PSDataCollectionProfile.DefaultFileName); if (!store.DirectoryExists(Path.Combine(SSPowerShell.ProfileDirectory, product))) { store.CreateDirectory(Path.Combine(SSPowerShell.ProfileDirectory, product)); } string contents = JsonConvert.SerializeObject(profile); store.WriteFile(dataPath, contents); } catch { // do not throw for i/o or serialization errors } }
static PSDataCollectionProfile Initialize(string product, bool ignoreError = true) { PSDataCollectionProfile result = new PSDataCollectionProfile(); try { var environmentValue = Environment.GetEnvironmentVariable(PSDataCollectionProfile.EnvironmentVariableName); bool enabled = true; if (!string.IsNullOrWhiteSpace(environmentValue) && bool.TryParse(environmentValue, out enabled)) { result.EnableDataCollection = enabled; } else { var store = new DiskDataStore(); string dataPath = Path.Combine(SSPowerShell.ProfileDirectory, product, PSDataCollectionProfile.DefaultFileName); if (store.FileExists(dataPath)) { string contents = store.ReadFileAsText(dataPath); var localResult = JsonConvert.DeserializeObject <PSDataCollectionProfile>(contents); if (localResult != null && localResult.EnableDataCollection.HasValue) { result = localResult; } } else { WritePSDataCollectionProfile(product, new PSDataCollectionProfile(true)); } } } catch { // do not throw for i/o or serialization errors if (!ignoreError) { throw; } } return(result); }