所以File.WriteAllLines有这个问题。我想知道是否可以设置一个字符串变量来自动填充城市名称吗?如果是的话,我该怎么做?这是我的代码。
void GiveALLFreeMuseumsToFileTest(MuseumsContainer museums)
{
string[] lines = new string[museums.Count];
for (int i = 0; i < museums.Count; i++)
{
lines[i] = lines[i] + "Museum Name: " + museums.GetMuseum(i).Name;
lines[i] = lines[i] + "; Museum Type: " + museums.GetMuseum(i).Type;
if (museums.GetMuseum(i).Days.Count() <= 2 && (museums.GetMuseum(i).Days.Contains("VI") || museums.GetMuseum(i).Days.Contains("VII")))
{
lines[i] = lines[i] + "; Open only at WEEKENDS";
}
if (museums.GetMuseum(i).City == "NewYork")
{
File.WriteAllLines("Nemokami_NewYork.csv", lines);
}
if (museums.GetMuseum(i).City == "Washington")
{
File.WriteAllLines("Nemokami_Washington.csv", lines);
}
if (museums.GetMuseum(i).City == "Chicago")
{
File.WriteAllLines("Nemokami_Chicago.csv", lines);
}
if (museums.GetMuseum(i).City == "Charlotte")
{
File.WriteAllLines("Nemokami_Charlotte.csv", lines);
}
if (museums.GetMuseum(i).City == "Miami")
{
File.WriteAllLines("Nemokami_Miami.csv", lines);
}
}
}
答案 0 :(得分:2)
File.WriteAllLines($"Nemokami_{museums.GetMuseum(i).City}.csv", lines);
或旧式:
File.WriteAllLines("Nemokami_" + museums.GetMuseum(i).City + ".csv", lines);