Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Общие вопросы по .NET и C# > Работа c INI-файлами


Автор: Vyacheslav 1.3.2005, 13:04
Имеется ли класс, инкапсулирующий работу с ini- файлами?

Автор: Borisff2003 1.3.2005, 13:14
Насколько я знаю, нет.
В .NET принята иная концепция, использованние XML конфигурационных файлов

Если интересно копай в System.Configuration

Автор: AntonSaburov 1.3.2005, 13:46
У меня был когда-то проект - осталось вот это. Могу просто предложить покопаться

Вот конфиг в виде XML. Первая половина описывает те параметры (тэги), которые будут использоваться. Вторая половина - это уже непосредственно сами параметры.

Код

<?xml version="1.0" encoding="windows-1251"?>
<configuration>
<configSections>
 <sectionGroup name="PipetteSectionGroup">
  <section name="PipetteName"
   type="System.Configuration.SingleTagSectionHandler"/>
  <section name="SerialPortSection"
   type="System.Configuration.NameValueSectionHandler"/>
  <section name="DataBaseConnection"
   type="System.Configuration.SingleTagSectionHandler"/>
  <section name="FlashConnection"
   type="System.Configuration.SingleTagSectionHandler"/>
  <section name="PipetteConnection"
   type="System.Configuration.SingleTagSectionHandler"/>
  <section name="EmbeddedSoft"
   type="System.Configuration.SingleTagSectionHandler"/>
  <section name="Debug"
   type="System.Configuration.SingleTagSectionHandler"/>
 </sectionGroup>
</configSections>

<appSettings>
</appSettings>
 
<PipetteSectionGroup>
 <PipetteName name="NOVUS"/>
 <SerialPortSection>
  <add key="COM1" value="1"/>
  <!--add key="COM2" value="2"/>
  <add key="COM4" value="4"/-->
 </SerialPortSection>
 <DataBaseConnection source="MSAccess"
  connect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Pipetka.MDB"/>
 <!--DataBaseConnection source="SQLServer"
  connect="Server=(local);Database=Pipetka;Password=;User ID=sa"/-->
 <FlashConnection baud="115200"/>
 <PipetteConnection baud="9600"/>
 <EmbeddedSoft file="extens.xcl" dir="NEC"/>
 <Debug serial_log_flash="0" serial_log_remote="0"/>
</PipetteSectionGroup>
</configuration>


А вот и текст для работы с ним
Код

public class Config_Pipette
{
 public static string GetPipetteName()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/PipetteName");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  return (string)sampleTable["name"];
 }

 public static Array GetSerialPorts()
 {
  ArrayList tmp = new ArrayList();
  NameValueCollection nvc = null;
  try
  {
   nvc = (NameValueCollection)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/SerialPortSection");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(nvc==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  else
  {
   for(int i=0; i<nvc.Count; i++)
   {
    tmp.Add(int.Parse(nvc[i]));
   }
  }
  return tmp.ToArray();
 }

 public static void GetDataBaseConnection(out string source, out string connect)
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/DataBaseConnection");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  source = (string)sampleTable["source"];
  connect = (string)sampleTable["connect"];
 }

 public static string GetFlashBaudRate()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/FlashConnection");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  return (string)sampleTable["baud"];
 }

 public static string GetPipetteBaudRate()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/PipetteConnection");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  return (string)sampleTable["baud"];
 }

 public static string GetEmbeddedSoftDir()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/EmbeddedSoft");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  return (string)sampleTable["dir"];
 }

 public static string GetEmbeddedSoftFileExt()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/EmbeddedSoft");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  return (string)sampleTable["file"];
 }

 public static bool GetDebugSerialLogFlash()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/Debug");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  string tmp_log = (string)sampleTable["serial_log_flash"];
  return tmp_log.Equals("1") ? true : false;
 }

 public static bool GetDebugSerialLogRemote()
 {
  IDictionary sampleTable;
  try
  {
   sampleTable = (IDictionary)
    ConfigurationSettings.GetConfig("PipetteSectionGroup/Debug");
  }
  catch(ConfigurationException con_e)
  {
   throw new Exceptions.BadConfigException("Config file error:"+con_e.Message);
  }
  if(sampleTable==null)
  {
   throw new Exceptions.NoConfigException("Config file doesn't exist");
  }
  string tmp_log = (string)sampleTable["serial_log_remote"];
  return tmp_log.Equals("1") ? true : false;
 }
}

Автор: Vyacheslav 1.3.2005, 17:01
Вот что называется - упрощение работы smile. Мне нужно просто работать с ini-файлом, по скольку его проще набивать вручную (данные для тестировочного модуля). Во всех приличных библиотеках работа с такими файлами давно инкапсулирована. А теперь придется городить свой класс и терять время. Эх, где мой C++Builder.

Автор: Kurt 1.3.2005, 19:34
Ну, как вариант, можно из .NET-кода вызывать соотв. WinAPI функции. smile

Автор: Domestic Cat 1.3.2005, 19:42
http://www.codeproject.com/dotnet/XMLSettingsFile.asp

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)