繁体   English   中英

一键来自不同来源的多个值c#

[英]one key multiple values from different sources c#

我正在尝试制作一个ac#程序,该程序将为我比较两个文件,并告诉我特定部分的区别。 我已经能够在循环时将所需的部分放入变量中,但是现在我想将这些部分添加到每个文件具有3个值的键中,因此总体上包含6个值的键将在以后与彼此进行比较。 但是我只能使用我拥有的循环一次添加3个值,因此我需要能够将后3个值添加到键中,而不会覆盖前3个值。

来自文件的数据示例:

[\Advanced\Rules\Correlation Rules\Suspect_portscan\];
CheckDescription    =S  Detect Port scans;
Enabled =B  0;
Priority    =L  3;

我已经设法将所需的变量放入变量中,所以有了:字符串SigName为“ Suspect_portscan” Int Enabled,Priority,Blocking为0 3,分别为null。

然后,我想制作一个字典类型的东西,用一个键作为SigName,并将前三个值设为启用,优先级和阻止。 然后,当遍历第二个文件时,我想为已启用的优先级添加第二个文件设置,在最后3个值槽中阻止相同的SigName(因此对键)。

然后,我将其与自身进行比较,例如“ if signame(0)!= signame(3)”,因此,如果启用的文件1与启用的文件2不同,请记下并告诉我。

但是我遇到的问题是无法将数据放入字典或查找中,我完全感到困惑。 看来我应该使用带有值列表的字典,但无法在第二个循环中使用它。

谢谢。

码:

     static void Main()
    {
        Dictionary<string, List<int>> PolicyDictionary = new Dictionary<string, List<int>>(); 
        int Counter = 0, TempCounter = 0;
        String[] PolicyOne = System.IO.File.ReadAllLines(@"filepath");
        string[] PolicyTwo = System.IO.File.ReadAllLines(@"filepath2");

        foreach (string lines in PolicyOne)
        {
            if ((lines.Contains("CheckDescription")) && (!lines.Contains("userdefined network event template")))
            {//if true do this
                TempCounter = Counter;

                int FirstSlashes = (PolicyOne[(TempCounter - 1)]).IndexOf(@"\", 28);
                int LastSlashes = (PolicyOne[(TempCounter - 1)]).LastIndexOf(@"\");
                string SigName = (PolicyOne[(TempCounter - 1)]).Substring(FirstSlashes+1,(LastSlashes-FirstSlashes)-1);

               Char Enabled = (PolicyOne[(TempCounter + 1)])[(PolicyOne[(TempCounter + 1)]).Length - 2];
               Char Priority = (PolicyOne[(TempCounter + 2)])[(PolicyOne[(TempCounter + 2)]).Length - 2];
               Char Blocking = (PolicyOne[(TempCounter + 3)])[(PolicyOne[(TempCounter + 3)]).Length-2];

               PolicyDictionary[SigName] = new List<int> {Enabled,Priority,Blocking};



            }
           // end if
            Counter++;
        }
        // end for each

        foreach (string lines in PolicyTwo)
        {
            if ((lines.Contains("CheckDescription")) && (!lines.Contains("userdefined network event template")))
            {//if true do this
                Counter = 0;
                TempCounter = Counter;

                int FirstSlashes = (PolicyTwo[(TempCounter - 1)]).IndexOf(@"\", 28);
                int LastSlashes = (PolicyTwo[(TempCounter - 1)]).LastIndexOf(@"\");
                string SigName = (PolicyTwo[(TempCounter - 1)]).Substring(FirstSlashes + 1, (LastSlashes - FirstSlashes) - 1);

                Char Enabled = (PolicyTwo[(TempCounter + 1)])[(PolicyOne[(TempCounter + 1)]).Length - 2];
                Char Priority = (PolicyTwo[(TempCounter + 2)])[(PolicyOne[(TempCounter + 2)]).Length - 2];
                Char Blocking = (PolicyTwo[(TempCounter + 3)])[(PolicyOne[(TempCounter + 3)]).Length - 2];

                // here I will just end up overwriting it, but im not sure how to get arround this.
                PolicyDictionary[SigName] = new List<int> { Enabled, Priority, Blocking };


            }
            // end if
            Counter++;
        }
        // end for each

        Console.ReadKey();

我想我掌握了您要执行的操作,但是代码示例可能会有所帮助。

听起来您需要为要存储的值创建类型。

例如

public class FileContent
{
  public char Enabled {get; set;}
  public string Priority {get; set;}
  public string SigName {get; set; 

 /*whatever else you need*/
}

然后,您可以从每个文件(或多个文件)创建两个这种类型的实例。 如果要比较特定的属性,可以。 如果您需要存储在字典中,则可以。

或者,您可以重写Equals()或实现IEquatable以启用类型比较。

那当然是假设我了解这个问题。

由于我目前不知道您的代码是什么样子,而且我只有您的描述,所以我建议这样

public class FileData
{
  public int Enabled {get; set;}
  public int Priority {get; set;}
  public int Blocking {get; set;}
}

然后创建两个字典

var File1 = new Dictionary<string, Filedata>();
var File2 = new Dictionary<string, Filedata>();

在浏览两个文件时填写这两个,SigName是字典的键(字符串)

比较他们像这样:

foreach (var item in File1)
{
  var file1Data = item.Value;
  var file2Data = file2[item.Key];
  if (file1Data.Enabled != file2Data.Enabled)
  {
     // do what you want
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM