2013-09-09 7 views
2

У меня вопрос о чтении ini-файла, Мне нужно прочитать определенную часть файла ini im im и не могу понять, как это сделать, Я уже могу читать и пишите из и в файл ini, но мне нужно прочитать определенную часть.Прочитать определенную часть файла INI C#

Вот мой INI-файл:

[Settings] 

[ACR] 
11: Start Removal =90 // ms 
12: Removal Time =20 // commentary 
13: Removal Delay =2.1 // commentary 

[Cleaning] 
21: Dur. Cleaning =90 //commentary 
22: Time valve on =30 //commentary 
23: Time valve off =15 //commentary 

[Calibrate] 
31: Content left =100//commentary 
32: Calibrate left =--.-//commentary 
33: Content right =100//commentary 
34: Calibrate right =25.6//commentary 

[Conductivity] 
41: Factor left =500//commentary 
42: Offset left =220//commentary 
43: Factor right =500//commentary 
44: Offset right =40//commentary 
45: Level left =85//commentary 
46: Level right =85//commentary 

[General] 
51: Type of valve =5//commentary 
52: Indicator =2//commentary 
53: Inverse output =0//commentary 
54: Restart time =30//commentary 
55: Water time =0//commentary 
56: Gate delay =10//commentary 

[Pulsation] 
61: Pulsation p/m =60//commentary 
62: S/r ratio front =55//commentary 
63: S/r ratio back =60//commentary 
64: Stimulation p/m =200//commentary 
65: S/r stim front =30//commentary 
66: S/r stim back =30//commentary 
67: Stimulation dur =20//commentary 

Я должен прочитать первые 2 символа линии, так что позволяет так в разделе ACR мне нужно прочитать 10,11 и 12. и с очисткой раздела я должен прочитать 21,22,23 и так далее.

Это мой код до сих пор:

using System; 
using System.Windows.Forms; 
using Idento.Common.Utilities; 
using Milk_Units; 

namespace Milk_Units 
{ 
    public class SettingsIniFile 
    { 
     private const String FileNameCustom = "Data\\Custom.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),); 
     private const String FileNameDefault = "Data\\Default.ini";//Path.Combine(Path.GetDirectoryName(Application.ExecutablePath),); 


     public Settings LoadSettings(bool defaults = false) 
     { 
      String fileName = defaults ? FileNameDefault : FileNameCustom; 
      StringList input = new StringList().FromFile(fileName); 

      //Settings settings = null; 
      Settings settings = new Settings(); 

      foreach (var item in input) 
      { 
       String line = item.Trim(); 

       if (line.StartsWith("[") && line.EndsWith("]")) 
        continue; 

       int index = line.IndexOf('='); 
       if (index < 0) 
        continue; 

       String key = line.Substring(0, index).Trim(); 

       String value = line.Substring(index + 1).Trim(); 
       String comment = ""; 
       index = value.IndexOf("//"); 
       if (index > -1) 
       { 
        comment = value.Substring(index).Trim(); 
        value = value.Substring(0, index).Trim(); 
       } 

       // ACR 
       if (Utils.EqualsIgnoreCase(key, "10: Start Removal")) 
        settings.AcrStartRemoval = value; 
       else if (Utils.EqualsIgnoreCase(key, "11: Removal Time")) 
        settings.AcrRemovalTime = value; 
       else if (Utils.EqualsIgnoreCase(key, "12: Removal Delay")) 
        settings.AcrRemovalDelay = value; 
       // CLEANING 
       else if (Utils.EqualsIgnoreCase(key, "21: Dur. Cleaning")) 
        settings.CleanDurCleaning = value; 
       else if (Utils.EqualsIgnoreCase(key, "22: Time valve on")) 
        settings.CleanTimeValveOn = value; 
       else if (Utils.EqualsIgnoreCase(key, "23: Time valve off")) 
        settings.CleanTimeValveOff = value; 
       //CALIBRATE 
       else if (Utils.EqualsIgnoreCase(key, "31: Content left")) 
        settings.CalibrateContentLeft = value; 
       else if (Utils.EqualsIgnoreCase(key, "32: Calibrate left")) 
        settings.CalibrateCalibrateLeft = value; 
       else if (Utils.EqualsIgnoreCase(key, "33: Content right")) 
        settings.CalibrateContentRight = value; 
       else if (Utils.EqualsIgnoreCase(key, "34: Calibrate right")) 
        settings.CalibrateCalibrateRight = value; 
       //CONDUCTIVITY 
       else if (Utils.EqualsIgnoreCase(key, "41: Factor left")) 
        settings.ConductFactorLeft = value; 
       else if (Utils.EqualsIgnoreCase(key, "42: Offset left")) 
        settings.ConductOffsetleft = value; 
       else if (Utils.EqualsIgnoreCase(key, "43: Factor right")) 
        settings.ConductFactorRight = value; 
       else if (Utils.EqualsIgnoreCase(key, "44: Offset right")) 
        settings.ConductOffsetRight = value; 
       else if (Utils.EqualsIgnoreCase(key, "45: Level left")) 
        settings.ConductLevelLeft = value; 
       else if (Utils.EqualsIgnoreCase(key, "46: Level right")) 
        settings.ConductLevelRight = value; 
       //GENERAL 
       else if (Utils.EqualsIgnoreCase(key, "51: Type of valve")) 
        settings.GeneralTypeOfValve = value; 
       else if (Utils.EqualsIgnoreCase(key, "52: Indicator")) 
        settings.GeneralIndicator = value; 
       else if (Utils.EqualsIgnoreCase(key, "53: Inverse output")) 
        settings.GeneralInverseOutput = value; 
       else if (Utils.EqualsIgnoreCase(key, "54: Restart time")) 
        settings.GeneralRestartTime = value; 
       else if (Utils.EqualsIgnoreCase(key, "55: Water time")) 
        settings.GeneralWaterTime = value; 
       else if (Utils.EqualsIgnoreCase(key, "56: Gate delay")) 
        settings.GeneralGateDelay = value; 
       //PULSATION 
       else if (Utils.EqualsIgnoreCase(key, "61: Pulsation p/m")) 
        settings.PulsationPulsationPm = value; 
       else if (Utils.EqualsIgnoreCase(key, "62: S/r ratio front")) 
        settings.PulsationSrRatioFront = value; 
       else if (Utils.EqualsIgnoreCase(key, "63: S/r ratio back")) 
        settings.PulsationSrRatioBack = value; 
       else if (Utils.EqualsIgnoreCase(key, "64: Stimulation p/m")) 
        settings.PulsationStimulationPm = value; 
       else if (Utils.EqualsIgnoreCase(key, "65: S/r stim front")) 
        settings.PulsationSrStimFront = value; 
       else if (Utils.EqualsIgnoreCase(key, "66: S/r stim back")) 
        settings.PulsationSrStimBack = value; 
       else if (Utils.EqualsIgnoreCase(key, "67: Stimulation dur")) 
        settings.PulsationStimulationDur = value; 



      } 
      return settings; 
     } 

Спасибо заранее, и я знаю, им с помощью файла INI не правильно, но это лёгкий способ.

Awnser Спасибо Youn Neoistheone за помощь

 foreach (var item in input) 
        { 
         String line = item.Trim(); 

         if (line.StartsWith("[") && line.EndsWith("]")) 
          continue; 

         int index = line.IndexOf('='); 
         if (index < 0) 
          continue; 

         String key = line.Substring(0, index).Trim(); 
         String ID = line.Substring(0, line.IndexOf(':')); 
         String value = line.Substring(index + 1).Trim(); 
         //String comment = ""; 
         index = value.IndexOf("//"); 
         if (index > -1) 
         { 
         ID = line.Substring(0, line.IndexOf(':')); 
          //comment = value.Substring(index).Trim(); 
          value = value.Substring(0, index).Trim(); 
         } 



         // ACR 
         if (Utils.EqualsIgnoreCase(key, "11: Start Removal")) 
          { 
          settings.AcrStartRemoval11 = value; 
          _settings.AcrId11.ID 
          } 

настройки возврата; }

+0

https: //github.com/rickyah/ini-parser –

+2

Я настоятельно рекомендую рассмотреть возможность перехода к настройкам на основе app.config – Alex

+0

@DavidBrabant мне нужно заставить его работать в моей собственной программе или доступны источники –

ответ

2

Это будет делать это для вас:

var vals = File.ReadLines("C:\\TEMP\\test.ini") 
    .SkipWhile(line => !line.StartsWith("[ACR]")) 
    .Skip(1) 
    .TakeWhile(line => !string.IsNullOrEmpty(line)) 
    .Select(line => new 
    { 
     Key = line.Substring(0, line.IndexOf(':')), 
     Value = line.Substring(line.IndexOf(':') + 2) 
    }); 

ОБЪЯСНЕНИЕ

  • .SkipWhile(line => !line.StartsWith("[ACR]")) пропускает, пока не найдет секцию он хочет.
  • .Skip(1) пропускает эту строку, потому что мы действительно хотим читать значения.
  • .TakeWhile(line => !string.IsNullOrEmpty(line)) читает до тех пор, пока не найдет пустую строку.
  • .Select(line => new... выбирает значения каждой строки в анонимный тип.

Итак, только что в правом разделе вы получите любую секцию, которую хотите.

Другое преимущество этого подхода состоит в том, что это отсроченное исполнение, поэтому, если вам не нужно читать весь файл, чтобы найти раздел, он не будет.

Теперь в вашем случае вам может понадобиться немного массажа, чтобы убедиться, что он не читает комментарии в конце строки, например, изменив последний Substring. Это действительно зависит от реальных потребностей домена , но это не будет большой модификацией.

Вы можете, конечно, изменить это, а также удовлетворить любые другие запросы, которые вам нужны.

+0

это не работает :(потому что весь раздел ACR больше не будет показан. –

+0

@ user2721466, что? –

+0

Я реализовал вашу часть кода и протестировал его, но когда я запустил программу, он не прочитал бы весь раздел [ACR] из ini –

1

и я знаю, им с помощью файла INI не правильно, но это Самым лёгким способом.

Я думаю, что это не самый простой способ, попробовать что-то уже реализовано: GetPrivateProfileSection.

Есть 2 полезны окна API для чтения/записи INI файлы:

GetPrivateProfileString

WriteProfileString

проверить их!

+0

Как я уже сказал, мне нужно только прочитать определенную часть строк, и я уже могу читать и писать файл. Но я должен знать, как я могу прочитать первый символ персонажа. –

0

Вы можете использовать третий = класс партии, который считывает INI файлы:

http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C

+0

1966? Являются ли файлы ini старыми? :) – ya23

+1

@ ya23 Ну, нет ... Статья на самом деле с 2002 года, номер 1966 года в ссылке не имеет ничего общего с годом ... –

+0

Я знаю, отсюда «:)» в своем комментарии. Просто (бедная) шутка. – ya23

0

Как уже сказал, есть гораздо более эффективные способы, чтобы сохранить ваши настройки, чем псевдо-INI-файл, но если необходимо, вы можете использовать регулярное выражение для разбора ваших строк:

void Main() 
{ 

    string line1 = "[ACR]"; 
    string line2 = "53: Inverse output =0.3//commentary"; 

    var a = Regex.Match(line1,@"^\[(.*)\]").Groups; 
    var b = Regex.Match(line2,@"^(\d\d):\s*(.*)\s*=(.*)//(.*)").Groups; 

    string sectionName = a[1].Value; 

    string number = b[1].Value; 
    string setting = b[2].Value; 
    string value = b[3].Value; 
    string comment = b[4].Value; 

} 
1
    ` namespace TeknoModding 
{ 
    using System; 
    using System.IO; 
    using System.Collections; 

public class IniFile 
{ 
    private readonly Hashtable _keyPairs = new Hashtable(); 
    private readonly String _iniFilePath; 

    private struct SectionPair 
    { 
     public String Section; 
     public String Key; 
    } 

    /// <summary> 
    /// Opens the INI file at the given path ;and enumerates the values in the IniParser. 
    /// </summary> 
    /// <param name="iniPath">Full path to INI file.&lt;/param> 
    public IniParser(String iniPath) 
    { 
     TextReader iniFile = null; 
     String strLine = null; 
     String currentRoot = null; 
     String[] keyPair = null; 

     this._iniFilePath = iniPath; 

     if(File.Exists(iniPath)) 
     { 
      try 
      { 
       iniFile = new StreamReader(iniPath); 

       strLine = iniFile.ReadLine(); 

       while (strLine != null) 
       { 
        strLine = strLine.Trim(); 

        if (strLine != "") 
        { 
         if (strLine.StartsWith("[") && strLine.EndsWith("]")) 
         { 
          currentRoot = strLine.Substring(1, strLine.Length - 2); 
         } 
         else 
         { 
          keyPair = strLine.Split(new char[]{ '=' }, 2); 

          SectionPair sectionPair; 
          String value = null; 

          if (currentRoot == null) 
           currentRoot = "ROOT"; 

          sectionPair.Section = currentRoot; 
          sectionPair.Key = keyPair[0]; 

          if (keyPair.Length > 1) 
           value = keyPair[1]; 

          this._keyPairs.Add(sectionPair, value); 
         } 
        } 

        strLine = iniFile.ReadLine(); 
       } 

      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
      finally 
      { 
       if (iniFile != null) 
        iniFile.Close(); 
      } 
     } 
     else 
      throw new FileNotFoundException("Unable to locate " + iniPath); 

    } 

    /// <summary> 
    /// Returns the ;value for the given section, key pair. 
    /// </summary> 
    /// <param name="sectionName">Section name.</param> 
    /// <param name="settingName">Key name.</param> 
    public string GetSetting(string sectionName, string settingName) 
    { 
     try 
     { 
     SectionPair sectionPair; 
     sectionPair.Section = sectionName; 
     sectionPair.Key = settingName; 

     return (String)this._keyPairs[sectionPair]; 
     } 
     catch (Exception) 
     { 
      return string.Empty; 
     } 
    } 

    /// <summary> 
    /// Enumerates alllines for given section. 
    /// </summary> 
    /// <param name="sectionName">Section to enum.</param>; 
    public string[] EnumSection(string sectionName) 
    { 
     ArrayList tmpArray = new ArrayList(); 

     foreach (SectionPair pair in this._keyPairs.Keys) 
     { 
      if (pair.Section == sectionName) 
       tmpArray.Add(pair.Key); 
     } 

     return (String[])tmpArray.ToArray(typeof(String)); 
    } 

    /// <summary> 
    /// Adds or replaces a setting to the tableto be saved. 
    /// </summary> 
    /// <param name="sectionName">Section to add under.<;/param> 
    /// <param name="settingName">Key name to add.</param> 
    /// <param name="settingValue">Value of key.</param> 
    public void AddSetting(string sectionName, string settingName, string settingValue) 
    { 
     SectionPair sectionPair; 
     sectionPair.Section = sectionName; 
     sectionPair.Key = settingName; 

     if(this._keyPairs.ContainsKey(sectionPair)) 
      this._keyPairs.Remove(sectionPair); 

     this._keyPairs.Add(sectionPair, settingValue); 
    } 

    /// <summary> 
    /// Adds or replaces a setting to the tableto be saved with a nullvalue. 
    /// </summary> 
    /// <param name="sectionName">Section to add under.<;/param> 
    /// <param name="settingName">Key name to add.</param> 
    public void AddSetting(string sectionName, string settingName) 
    { 
     AddSetting(sectionName, settingName, null); 
    } 

    /// <summary> 
    /// Remove a setting. 
    /// </summary> 
    /// <param name="sectionName">Section to add under.<;/param> 
    /// <param name="settingName">Key name to add.</param> 
    public void DeleteSetting(string sectionName, string settingName) 
    { 
     SectionPair sectionPair; 
     sectionPair.Section = sectionName; 
     sectionPair.Key = settingName; 

     if(this._keyPairs.ContainsKey(sectionPair)) 
      this._keyPairs.Remove(sectionPair); 
    } 

    /// <summary> 
    /// Save settingsto new file. 
    /// </summary> 
    /// <param name="newFilePath">New file path.</param> 
    public void SaveSettings(string newFilePath) 
    { 
     ArrayList sections = new ArrayList(); 
     string tmpValue = ""; 
     string strToSave = ""; 

     foreach (SectionPair sectionPair in this._keyPairs.Keys) 
     { 
      if (!sections.Contains(sectionPair.Section)) 
       sections.Add(sectionPair.Section); 
     } 

     foreach (string section in sections) 
     { 
      strToSave += ("[" +section + "]\r\n"); 

      foreach (SectionPair sectionPair in this._keyPairs.Keys) 
      { 
       if (sectionPair.Section == section) 
       { 
        tmpValue = (String)this._keyPairs[sectionPair]; 

        if (tmpValue != null) 
         tmpValue = "=" + tmpValue; 

        strToSave += (sectionPair.Key + tmpValue + "\r\n"); 
       } 
      } 

      strToSave += "\r\n"; 
     } 

     try 
     { 
      TextWriter tw = new StreamWriter(newFilePath); 
      tw.Write(strToSave); 
      tw.Close(); 
     } 
     catch(Exception ex) 
     { 
      throw ex; 
     } 
    } 

    /// <summary> 
    /// Save settingsback to ini file. 
    /// </summary> 
    public void SaveSettings() 
    { 
     SaveSettings(this._iniFilePath); 
    }` 
} 
}