是否可以将字符串数组列表写入txt文件?
这是我的代码,我不知道如何做最后一步。我根据用户输入的数字制作了一个列表I foreach
,并且它正确地返回了所有值,但我似乎无法将所有数据保存到txt文件。 / p>
static void Main(string[] args)
{
Console.WriteLine("Unesi broj:");
int userNumber = Convert.ToInt32(Console.ReadLine());
var numberLIst = Enumerable.Range(1,userNumber).ToList();
string path = @"C:\Users\mcrnic\Desktop\scriptFolder\input.txt";
List<string[]> listaStringova = new List<string[]>();
int n = 0;
foreach (var data in numberLIst)
{
n++;
#region Varijable
var textBoxNumberRangeFirst = "tbxNumberRangeFirstInput" + data;
var textBoxNumberRangeSecond = "tbxNumberRangeSecondInput" + data;
var textBoxNumberRangeThird = "tbxNumberRangeThirdInput" + data;
var textBoxHomeNumbersFirst = "tbxRangeOfHomeNumbersFirstInput" + data;
var textBoxHomeNumbersSecond = "tbxRangeOfHomeNumbersSecondInput" + data;
var textBoxVPNFirst = "tbxRangeOfShortVPNNumbersFirstInput" + data;
var textBoxVPNSecond = "tbxRangeOfShortVPNNumbersSecondInput" + data;
var checkBoxNumberTransfer = "cbxNumberTransfer" + data;
var checkBoxSecretNumber = "cbxSecretNumber" + data;
var checkBoxSearchByNumber = "cbxSearchByNumber" + data;
var checkBoxZ1 = "cbxZ1_" + data;
var checkBoxZ2 = "cbxZ2_" + data;
var checkBoxZ3 = "cbxZ3_" + data;
var checkBoxZ4 = "cbxZ4_" + data;
var checkBoxZ5 = "cbxZ5_" + data;
var checkBoxZ6 = "cbxZ6_" + data;
var checkBoxZ7 = "cbxZ7_" + data;
#endregion
string[] htmlLinije = {"<%-- Row" + n + " --%>",
"<div class=&row rowFullWidth form-inline pb10& hidden>",
"<div class=&col-sm-4&>",
"<asp:TextBox runat=&server& ID=&" + textBoxNumberRangeFirst + "& class=&form-control textbox-width-25 ml10& ReadOnly=&true& />",
"<span class=&pr5 pl5&>/</span><asp:TextBox runat=&server& ID=&" + textBoxNumberRangeSecond +"& class=&form-control textbox-width-25& ReadOnly=&true&/>",
"<span class=&pr5 pl5&>-</span><asp:TextBox runat=&server& ID=&" + textBoxHomeNumbersSecond + "& class=&form-control textbox-width-25& ReadOnly=&true&/>",
"</div>",
"<div class=&col-sm-2&>",
"<asp:CheckBox runat=&server& ID=&" + checkBoxNumberTransfer + "& AutoPostBack=&false& CssClass=&checkbox pl20 pr15 pt5& Enabled=&false&/>",
"<asp:CheckBox runat=&server& ID=&" + checkBoxSecretNumber + "& AutoPostBack=&false& CssClass=&checkbox pl20 pr15 pt5& Enabled=&false&/>",
"<asp:CheckBox runat=&server& ID=&" + checkBoxSearchByNumber + "& AutoPostBack=&false& CssClass=&checkbox pl20 pr15 pt5& Enabled=&false& />",
"</div>",
"<div class=&col-sm-2&>",
"<asp:TextBox runat=&server& ID=&" + textBoxHomeNumbersFirst + "& class=&form-control textbox-width-40& ReadOnly=&true& /><span class=&pr10 pl10&>-</span>",
"<asp:TextBox runat=&server& ID=&" + textBoxHomeNumbersSecond + "& class=&form-control textbox-width-40& ReadOnly=&true& />",
"</div>",
"<div class=&col-sm-2&>",
"<asp:TextBox runat=&server& ID=&" + textBoxVPNFirst + "& class=&form-control textbox-width-40& ReadOnly=&true& /><span class=&pr10 pl10&>-</span>",
"<asp:TextBox runat=&server& ID=&" + textBoxVPNSecond + "& class=&form-control textbox-width-40& ReadOnly=&true& />",
"</div>",
"<div class=&col-sm-2&>",
"<div class=&pt5&>",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ1 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ2 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ3 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ4 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ5 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ6 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"<asp:CheckBox runat=&server& ID=&" + checkBoxZ7 + "& AutoPostBack=&false& CssClass=&checkbox pr5& Enabled=&false& />",
"</div>",
"</div>",
"</div>",
""
};
listaStringova.Add(htmlLinije);
}
System.IO.File.WriteAllLines(path, listaStringova);
}
当我尝试WriteAllLines
时,我收到此错误:
错误CS1503参数2:无法从System.Collections.Generic.List转换为string []
答案 0 :(得分:2)
WriteAllLines
期望一个字符串数组作为第二个参数。您正在发送一个字符串数组数组,因此您需要先将此列表展平。
这可以使用SelectMany
完成,如下所示:
var stringlist = listaStringova.SelectMany(line => line);
System.IO.File.WriteAllLines(path, stringlist);
答案 1 :(得分:1)
您正在尝试将字符串数组列表写入该文件。您必须编写一个字符串列表(每行一个字符串):
List<string> listStringova = new List<string>();
答案 2 :(得分:1)
您正在尝试为List创建WriteAllLines,而不是字符串[]。根据MSDN,WriteAllLines的唯一重载需要IEnumerable<String>
,而不是IEnumerable<String[]>
。此外,您应该正确使用StringBuilder
类,然后执行WriteAllText
。这对你有很大的帮助。
答案 3 :(得分:0)
更改此行:
List<string[]> listaStringova = new List<string[]>();
要:
List<string> listaStringova = new List<string>();
这就是你如何声明一个字符串列表。然后改变这个:
listaStringova.Add(htmlLinije);
要:
listaStringova.AddRange(htmlLinije);
这就是如何将数组的所有项目添加到列表中。
答案 4 :(得分:0)
您只需使用以下一行代码:
System.IO.File.WriteAllLines(path, listaStringova.SelectMany(i => i));