Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向CSV文件中添加字符串,同时监视CSV文件中的现有字符串组合_C#_Csv - Fatal编程技术网

C# 向CSV文件中添加字符串,同时监视CSV文件中的现有字符串组合

C# 向CSV文件中添加字符串,同时监视CSV文件中的现有字符串组合,c#,csv,C#,Csv,我正在尝试编写一个C#方法,它将向CSV文件添加一组字符串。就是这样, public void StoreWords(string expectedValue, stringActualValue) { ///Store expectedValue, actualValue, and a seed value per row in the /// .csv file. If the combination of the expectValue and actualValue ///d

我正在尝试编写一个C#方法,它将向CSV文件添加一组字符串。就是这样,

public void StoreWords(string expectedValue, stringActualValue)
{
  ///Store expectedValue, actualValue, and a seed value per row in the
  /// .csv file. If the combination of the expectValue and actualValue
  ///does not exist in the .csv file, then initialize a seed value for that 
  ///combination, otherwise increment the seed value for that combination
  }

我被困在打开csv文件和存储单词组合上。任何帮助都将不胜感激。

我认为代码应该满足您的要求:

private string FilePath { get; set; }

private string[] ReadFile()
{
    return File.ReadAllLines(FilePath);
}

public Dictionary<Tuple<string, string>, string> MakeOrIncSeedValuesFromCsv(string[] lines)
{
    var valuePairToSeedValue = new Dictionary<Tuple<string, string>, string>();

    var lines = ReadFile();
    for (int i = 0; i < lines.Length; i++)
    {
        var values = lines[i].Split(',');

        if (values.Length > 2)
        {
            var newSeedValue = IncSeedValue(lines[2]);
            var key = Tuple.Create<string, string>(values[0], values[1]);

            if (!valuePairToSeedValue.ContainsKey(key))
            {
                valuePairToSeedValue.Add(key, newSeedValue);
            }
            else
            {
                valuePairToSeedValue[key] = newSeedValue;
            }

        }
        else
        {
            var key = Tuple.Create<string, string>(values[0], values[1]);
            var seed = GetSeedValue(key);

            if (!valuePairToSeedValue.ContainsKey(key))
            {
                valuePairToSeedValue.Add(key, seed);
            }
            else
            {
                valuePairToSeedValue[key] = seed;
            }
        }
    }

    return valuePairToSeedValue;
}

private string GetSeedValue(Tuple<string, string> values)
{
    return // put your code here
}

private string IncSeedValue(string actualSeedValue)
{
    return // put your code here
}
私有字符串文件路径{get;set;}
私有字符串[]ReadFile()
{
返回File.ReadAllLines(文件路径);
}
公共字典MakeOrIncSeedValuesFromCsv(字符串[]行)
{
var valuePairToSeedValue=新字典();
var lines=ReadFile();
对于(int i=0;i2)
{
var newSeedValue=IncSeedValue(第[2]行);
var key=Tuple.Create(值[0],值[1]);
如果(!valuePairToSeedValue.ContainsKey(键))
{
valuePairToSeedValue.Add(键,newSeedValue);
}
其他的
{
valuePairToSeedValue[键]=newSeedValue;
}
}
其他的
{
var key=Tuple.Create(值[0],值[1]);
var seed=GetSeedValue(键);
如果(!valuePairToSeedValue.ContainsKey(键))
{
valuePairToSeedValue.Add(键,种子);
}
其他的
{
valuePairToSeedValue[键]=种子;
}
}
}
返回值PairToSeedValue;
}
私有字符串GetSeedValue(元组值)
{
return//将代码放在这里
}
私有字符串IncSeedValue(字符串实际值)
{
return//将代码放在这里
}

}

问题太广泛(不止一个问题…)。首先搜索“如何在c#中读取文件”。然后搜索“如何附加到c#中的文件”。还有可能是“如何在c#中解析csv文件”。然后试试你所学的,如果你被卡住了,带着一个特定的问题回来。当我尝试使用它时,代码中有一个小问题。在MakeOrIncSeedValuesFromCsv方法参数中,有字符串[]行。但是在方法内部有var行。这让我和编者感到困惑。在声明的for循环中,循环应该增加到的限制基于line.Length。您指的是哪一行?另外,我能够实现IncSeedValue(stringactualseedvalue)。但是我对私有字符串GetSeedValue(元组值)有问题。请从方法中删除此参数。好的。