从 RichTextBox 中的每一行获取文本?

Get text from each line in RichTextBox?

我想从 RichTextBox 中的每一行中提取一个文本,并在此文本行的开头和结尾添加一个常量文本。当我按下按钮时,新文本将 exported/saved 到文本文件。我能够创建一个循环,但如何在开头和结尾添加条件?

private void button1_Click(object sender, EventArgs e)
{   
    string text1 = "hello";
    string text2 = richboxtext.Text;
    string text3 = "goodbye";

    for (int i = 0; i < text2.Length; i++)
    {
        if(...)
        {
           ...
        }
    }
    File.WriteAllText(@"C:\Temp\exapmle.txt", text);
}

只需遍历 Lines 个字符串数组:

string textb = "hello";            
string texte = "goodbye";
var sb = new StringBuilder();
foreach(var line in richTextBox1.Lines)
{
    sb.AppendLine(textb + line + texte);
}
File.WriteAllText(@"C:\Temp\exapmle.txt", sb.ToString());

我想分享我在您的帮助下开发的代码的最终版本。

利用这段代码,我开发了一个脚本,将每条线路的IP地址批量添加到Fortinet。并且我开发了一个脚本来将相同的IP地址集中分配给一个组。

 private void button1_Click(object sender, EventArgs e)
        {
            string conf1 = "# config firewall address"; 
            string code1 = "    edit " ;
            string code2 = Environment.NewLine + "        set subnet ";
            string code3 = "/32";
            string code4 = Environment.NewLine + "    next" ;
            string conf2 = "end";
            string conf3 = "# config firewall addrgrp";
            string code5 = Environment.NewLine + "    edit test-grp";
            string code6 = Environment.NewLine + "        set member ";
            string code7 = Environment.NewLine + "    next";
            string conf4 = "end";
            var ipadress = new StringBuilder();
            foreach (var line in adresler.Lines)
            {
                ipadress.AppendLine(code1 + line + code2 + line + code3 + code4);
            }
            var ipgroup = new StringBuilder();
            foreach (var line in adresler.Lines)
            {
                ipgroup.Append(line + " ");
            }
            File.WriteAllText(@"C:\Temp\exapmle.txt", conf1 + Environment.NewLine + ipadress.ToString() + Environment.NewLine + conf2);
            File.WriteAllText(@"C:\Temp\exapmle2.txt", conf3 + code5 + code6 + ipgroup.ToString() + code7 + Environment.NewLine + conf4);

        }