【发布时间】:2022-11-20 18:30:33
【问题描述】:
离开 C# 一段时间后,我又回来了,但是这个让我很困惑。
我的网站上有一个 .txt 文件 (strRunnerTXTFile),它的内容被读入一个变量。
文本文件(读入 strRunnerTXTFile)包含以下内容:
"""BrandName"""
No
"""Brand\\Runner 1 Front"""
150mm
"""Brand\\Runner 2 Front"""
198mm
"""Brand\\Runner 3 Front"""
230mm
"""Brand\\Runner 4 Front"""
将其读入变量后,上面的代码现在如下所示:
"""BrandName"""
No
"""Brand\\Runner 1 Front"""
150mm
"""Brand\\Runner 2 Front"""198mm
"""Brand\\Runner 3 Front"""
230mm
"""Brand\\Runner 4 Front"""
我用来将文件读入变量的代码是这样的:
WebClient wc = new WebClient(); // create object to use to access web data
byte[] raw = wc.DownloadData(strRunnerTXTFile); // read data text file into variable
if (raw.Length == 0) { ExitWithError("Could not source data from server, or data file is empty.", 5); }
string webData = System.Text.Encoding.UTF8.GetString(raw); // convert into usable format
string[] strTXTInput = webData.Split('\n'); // split array into indexes by new line separation
sRunnerSetName = strTXTInput[0].Replace("\"","");
for (x = 0; x < strTXTInput.Length-1; x++)
{
switch (x)
{
case 0:
sRunnerSetName = strTXTInput[x];
break;
case 1:
sFH = strTXTInput[x];
break;
case 2:
sR1 = strTXTInput[x];
break;
case 3:
sH2 = strTXTInput[x];
break;
case 4:
sR2 = strTXTInput[x];
break;
case 5:
sH3 = strTXTInput[x];
break;
case 6:
sR3 = strTXTInput[x];
break;
case 7:
sH4 = strTXTInput[x];
break;
case 8:
sR4 = strTXTInput[x];
break;
case 9:
sH5 = strTXTInput[x];
break;
case 10:
sR5 = strTXTInput[x];
break;
default:
break;
}
}
createOutputString(RunnerSetFile);
然后后来......
public static void createOutputString(string RunnerSetFile)
{
List<Item> list = new List<Item>
{
new Item { Description = sRunnerSetName, SortOrder = iRunnerSetName },
new Item { Description = sFH, SortOrder = iFH },
new Item { Description = sR1, SortOrder = iR1 },
new Item { Description = sH2, SortOrder = iH2 },
new Item { Description = sR2, SortOrder = iR2 },
new Item { Description = sH3, SortOrder = iH3 },
new Item { Description = sR3, SortOrder = iR3 },
new Item { Description = sH4, SortOrder = iH4 },
new Item { Description = sR4, SortOrder = iR4 },
new Item { Description = sH5, SortOrder = iH5 },
new Item { Description = sR5, SortOrder = iR5 }
};
list = list.OrderBy(x => x.SortOrder).ToList();
}
它似乎是最后一行的东西,在那里它对顺序进行排序。但对于我的生活,我无法弄清楚为什么它会结合两条线。希望你们中的一个人能帮我解决这个问题?
【问题讨论】:
-
您需要检查
string的实际内容并查看换行符实际包含的内容。 -
你为什么要使用那个愚蠢的
for和switch?只需为从 0 到 10 的每个索引编写一个作业。 -
文本文件中的数据可能并不总是与我创建输出文件时所需的顺序相同,因此它必须通过处理器运行以将每个项目放入单独的变量中。
标签: c# variables text-files