Home > Mobile >  Get text from each line in RichTextBox?
Get text from each line in RichTextBox?

Time:11-04

I want to take a text from each line in the RichTextBox and add a constant text to the beginning and end of this text line. And when I press the button, the new text will be exported/saved to a text file. I was able to create a loop, but how can I add a condition to the beginning and the end?

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);
}

CodePudding user response:

Just loop over the Lines strings array:

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());

CodePudding user response:

I would like to share the final version of the code I developed with your help.

With this code, I developed a script to add the IP address of each line to the Fortinet in bulk. And I developed a script to collectively assign the same IP addresses to a group.

 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);

        }
  • Related