我想從 中的每一行中取出一個文本,RichTextBox并在該文本行的開頭和結尾添加一個常量文本。當我按下按鈕時,新文本將被匯出/保存到文本檔案中。我能夠創建一個回圈,但是如何在開頭和結尾添加條件?
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);
}
uj5u.com熱心網友回復:
只需回圈遍歷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());
uj5u.com熱心網友回復:
我想分享我在您的幫助下開發的代碼的最終版本。
使用這段代碼,我開發了一個腳本,將每一行的 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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/347995.html
上一篇:為什么我收到例外System.Reflection.TargetInvocationException:'Exceptionhasbeenthrowedbythetargetofaninvo
