C# 打印C中某行上方的一行#

C# 打印C中某行上方的一行#,c#,C#,我有一个文本文件,它包括8000多行。我需要打印一个特定的行,也打印另一行,这是第一行的前一行。问题是第二行没有uniqe特性,所以我不能像第一行那样调用它 总结 : --第二线 --一线 : 如何使用第一行获得第二行 我知道我应该反击,抓住第二条线,但我不知道怎么做。我感谢任何帮助 static string GetPreviousLine(string[] lines) { string temp = ""; foreach (string line in lines)

我有一个文本文件,它包括8000多行。我需要打印一个特定的行,也打印另一行,这是第一行的前一行。问题是第二行没有uniqe特性,所以我不能像第一行那样调用它

总结

:

--第二线

--一线

:

如何使用第一行获得第二行

我知道我应该反击,抓住第二条线,但我不知道怎么做。我感谢任何帮助

static string GetPreviousLine(string[] lines)
{
    string temp = "";
    foreach (string line in lines)
    {
        if (line == "First Line") return temp;
        else temp = line;
    }
    throw new Exception("not found");
}

我们读取每一行并将其保存到
temp
。然后我们读下一行,如果它是“代码>第1行<代码>,我们知道<代码> TEP< <代码>是代码>第2行<代码>,否则我们将新的行保存到<代码> TEMP并以相同的方式继续。

< P>我创建了一个.txt文件,其中一行和掩埋在中间的是两行,第一行是“弗兰克”,第二行是“球”。要打印“Frank Ball”,请尝试以下操作:

string line;
        string line1;
        string line2;

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        //walk the file line by line
        while ((line = file.ReadLine()) != null)
        {

            if(line.Contains("Ball"))
            {
                //Once you find your search value, set line2 to the line and stop walking the file.
                line2 = line;
                break;
            }
            //set line1 to the line value to hold onto it if you find your search value

            line1 = line;
        }
       //Now you have both strings and you can concatenate them however you want and print them
        string s = line1 + " " + line2;

        PrintDocument p = new PrintDocument();
        p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

        };
        try
        {
            p.Print();
        }
        catch (Exception ex)
        {
            throw new Exception("Exception Occured While Printing", ex);
        }

        file.Close();
我知道我应该反击,抓住第二条线,但我不知道怎么做

要计算行数,只需在方法
System.IO.File.ReadAllLines(filePath)
返回的
数组上使用
for
循环即可。这将在每次迭代中递增,您可以使用
行[i-1]
访问前一行

下面是一个方法,该方法将使用
for
循环搜索字符串数组,查找与特定字符串匹配的行(本例中不区分大小写),如果找到搜索项,则返回上一个数组项:

private static string GetLineBefore(string lineToFind, string[] lines)
{
    // Argument validation to avoid NullReferenceException or unnecessary search
    if (lines != null && !string.IsNullOrEmpty(lineToFind))
    {
        // Start at the second line (index 1) since there is no line before the first
        for (int i = 1; i < lines.Length; i++)
        {
            if (lines[i].Equals(lineToFind, StringComparison.OrdinalIgnoreCase))
            {
                // Since 'i' represents the index of the first line that we were
                // searching for, we return the previous item, at index 'i - 1'
                return lines[i - 1];
            }
        }
    }

    // If we never found our first line, return null to indicate no results
    return null;
}

打印比如打印机,还是文件?显示您尝试的代码。我知道我应该说谁?作业?如何确定需要打印的“特定行”@3Dave我在哪里按值传递数组?@3Dave错误。这与
列表
完全相同。两者都是通过引用传递的。我的错。没关系。我在C++领域已经有一段时间了。来回移动有时很麻烦。@3Dave我本来想评论一下,但你的个人资料显示C#是最高的标记。我猜C++确实会引起混乱:这太好笑了。我不会跳到GDI去“打印”一些东西,但我想它是有效的它快速、简单,而且几乎是防弹的。是的,我投了更高的票
var filePath = @"f:\public\temp\temp.txt";
var searchLine = "This is the text for the first line that I want to find";

// This will represent the line before the first one above
var previousLine = GetLineBefore(searchLine, File.ReadAllLines(filePath));