// ---------------------------------------------------------------------------------------------------- // Pre-Processing Operations // ---------------------------------------------------------------------------------------------------- // (None) // ---------------------------------------------------------------------------------------------------- // Input Processing Operations // ---------------------------------------------------------------------------------------------------- protected override void ProcessRecord() { foreach (var filepath in SessionLocation.GetResolvedPath(this.SessionState, this.Path)) { // Validation (File Existence Check): if (!File.Exists(filepath)) { throw new FileNotFoundException(); } // Open INI File Stream by READ-WRITE Mode using (var profile = new PrivateProfile(filepath, false)) { // SECTION Existence Check: if (profile.Contains(this.Section)) { // UPDATE SECTION this.Section = (from section_name in profile.Sections.Keys where string.Compare(section_name, this.Section, true) == 0 select section_name).First(); // KEY Existence Check) if (profile.Sections[this.Section].Entries.ContainsKey(this.Key)) { // UPDATE KEY this.Key = (from key in profile.Sections[this.Section].Entries.Keys where string.Compare(key, this.Key, true) == 0 select key).First(); } } // Should Process if (this.ShouldProcess($"ファイル '{filepath}'", $"セクション '{this.Section}' に含まれるキー '{this.Key}' の値の設定")) { // SET & Write VALUE profile.SetValue(this.Section, this.Key, this.Value).Write(); } } } }
// ---------------------------------------------------------------------------------------------------- // Post-Processing Operations // ---------------------------------------------------------------------------------------------------- // (None) // ---------------------------------------------------------------------------------------------------- // Private Method(s) // ---------------------------------------------------------------------------------------------------- // Write Profile according parameter private void WriteProfile(PrivateProfile profile, string target) { if (this.Section is null) { // ALL (FILE): // Should Process if (this.ShouldProcess(target, "全てのエントリーの取得")) { // GET Sections var sections = this.GetPrivateProfileSections(profile); // OUTPUT Sections this.WriteObject(sections); } } else { // Validation (SECTION Existence Check): if (!profile.Contains(this.Section)) { throw new KeyNotFoundException(); } // UPDATE SECTION this.Section = (from section_name in profile.Sections.Keys where string.Compare(section_name, this.Section, true) == 0 select section_name).First(); if (this.Key is null) { // SECTION: // Should Process if (this.ShouldProcess(target, $"セクション '{this.Section}' に含まれる全てのエントリーの取得")) { // GET Entries var entries = GetPrivateProfileEntries(profile.Sections[this.Section]); // OUTPUT Entries this.WriteObject(entries); } } else { // ENTRY: // Validation (KEY Existence Check): if (!profile.Sections[this.Section].Entries.ContainsKey(this.Key)) { throw new KeyNotFoundException(); } // UPDATE KEY this.Key = (from key in profile.Sections[this.Section].Entries.Keys where string.Compare(key, this.Key, true) == 0 select key).First(); // Should Process if (this.ShouldProcess(target, $"セクション '{this.Section}' に含まれるキー '{this.Key}' の値の取得")) { // OUTPUT Entry this.WriteObject(profile.GetValue(this.Section, this.Key)); } } } }