使用分隔符读取INI文件部分到数组

时间:2015-05-26 14:32:54

标签: c# file csv ini

我试图将我的C ++代码转换为Visual C#。我的目标是能够读取一些部分的INI文件(不是XML文件!)。每一行都分配给一个按钮,每个按钮都会启动一个命令。每个INI行有4个值,用";#34;。

分隔

例如:

  

[Button_section1]

     

Button1 = Button Scenario; open; notepad; / a; scenario.txt

意思是:

  • 列表项
  • Button1:按钮的真实姓名
  • 按钮场景:按钮的文本(dipslayed名称)
  • open:初始命令(也可以打印或探索......
  • 记事本:可执行文件(可以是任何文件或另一个例子的CMD)
  • / a:第一个参数
  • scenario.txt:第二个参数

因此,在我之前的代码中,我有25个方法ReadSectionStringSepare,它们能够读取我的INI文件的每个部分,然后使用StringSepare方法放入数组< / p>

加载表单时,可以使用以下

从INI文件中显示按钮文本
Button bt = (Button)this.Controls.Find(i, true)[0];

然后单击按钮允许启动以下命令: (在这种情况下打开) - &gt; notepad / a scenario.txt

注意: 我不想使用任何XML文件,因为我有一个与我的SQL BD的链接,它被解释为CSV文件。实际上,它是一个包含section。,..

的CSV文件

2 个答案:

答案 0 :(得分:2)

我也想插入自己的ini-parser库 https://github.com/rickyah/ini-parser

您可以使用NuGet安装它,支持Mono及其MIT许可,也非常容易使用

foreach(SectionData section in parsedData.Sections)
    foreach(KeyData key in section.Keys)

如果要收集有关每个按钮的数据,可以直接访问各部分中的部分和键:

<?php
$user = $_SESSION['user'];
    try{
        $results = $dbh->query("SELECT *
                                FROM groups
                                INNER JOIN user_group_link_table
                                ON groups.id = user_group_link_table.group_id
                                WHERE user_group_link_table.user_id = $user");

}catch(Exception $e) {
    echo $e->getMessage();
    die();
}

$group = $results->fetchAll(PDO::FETCH_ASSOC);

foreach($group as $groups){  

echo 

      $groups["name"]
      // show a link to admins that user do not see?
    ;}
?>

希望它对您有用,如果您有任何进步,我会接受PR;)

答案 1 :(得分:1)

您好Benji您可以使用我的库来帮助您处理INI文件: https://github.com/MarioZ/MadMilkman.Ini

例如:

// Set INI file's format options.
IniOptions option = new IniOptions();
// By default "CommentStarter" is ';' but you are using it as a
// multiple values separator so you need to change "CommentStarter"
option.CommentStarter = IniCommentStarter.Hash;

// Load INI file from path, Stream or TextReader.
IniFile ini = new IniFile(option);
ini.Load("Sample.ini");

// Select file's section.
IniSection sec = ini.Sections["Button_section1"];
// Select section's key.
IniKey key = sec.Keys["Button1"];
// Get key's values.
string[] values = key.Value.Split(';');

同样作为FYI,该库支持解析多个值(int-s,string-s,bool-s等),但语法不同,它识别以下内容:

Button1 = {Button Scenario,open,notepad,/ a,scenario.txt}

使用上述语法,您可以执行以下操作:

// Get key's values.
string[] values;
key.TryParseValue(out values);

尽管如此我希望它对您有所帮助,请注意您可以在以下链接的示例项目(C#和C ++ / CLI)中找到其他示例: https://github.com/MarioZ/MadMilkman.Ini/tree/master/MadMilkman.Ini.Samples

相关问题