如何在 C# 中检查 csv 行是否为空

问题描述 投票:0回答:1

我想知道如何用c#检查.csv文件中的csv行是否为空,以便我可以在其中写入数据。

空行是指空行,里面什么都没有。

如果你能帮我弄清楚如何在空行中书写,我会很有帮助

这是我的函数的开头,它使用 csv 文件并输出值

static public void ModuleSalarie()
    {
        string fileName = "Salarie.csv";
        string path = Path.Combine(Environment.CurrentDirectory, @"Data\", fileName);
        using (var reader = new StreamReader(path))
        {
            using (TextFieldParser parser = new TextFieldParser(reader))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    string[] ligne = parser.ReadFields();
                    foreach (string colonne in ligne)
                    {
                        Console.WriteLine(colonne);
                    }
                    Console.WriteLine();
                }

            }
        }
    }
c# csv
1个回答
0
投票
                string[] lines = File.ReadAllLines(path);
                int pm = Array.IndexOf(lines, "");

                if (pm != -1)
                {
                    lines[pm] = "Your text";
                }
                else
                {
                    Array.Resize(ref lines, lines.Length + 1);
                    lines[lines.Length - 1] ="Your text";
                }
                File.WriteAllLines(path, lines);
© www.soinside.com 2019 - 2024. All rights reserved.