我试图输入一个包含200个X,Y坐标的列表,然后输出一个包含这些坐标的命令列表C#



我正在尝试制作一个简单的工具Windows窗体应用程序,我可以粘贴一个200 X,Y坐标的列表,然后我可以单击一个按钮,该工具将输出一个文本文件,每行都有一个命令,其中包含X,Y座标。

输入如下所示:

65737,163129
-21687,-27399
164089,50153
164649,63465
-28663,140057
-28951,110329
149833,-30231

输出应该是这样的:

waypoint:WLM 1:1:78168:~:1560:1:true:0:gui.xaero_default:false:0:false
waypoint:WLM 2:2:919432:~:154200:11:true:0:gui.xaero_default:false:0:false
waypoint:WLM 3:3:791080:~:15624:0:true:0:gui.xaero_default:false:0:false
waypoint:WLM 4:4:79288:~:16968:6:true:0:gui.xaero_default:false:0:false
waypoint:WLM 5:5:79064:~:155702:9:true:0:gui.xaero_default:false:0:false

到目前为止,我已经能够将输入解析为数组,但现在我不知道如何将其组合并保存为文本文件。

private void button2_Click(object sender, EventArgs e)
{
var lines = richTextBox1.Text.Split((new char[] { ',' }), StringSplitOptions.RemoveEmptyEntries);
foreach (var s in lines)
{

string[] result = lines.ToArray();
outputwindow.Text = String.Join("n", result); // parse string so they are all on new lines ( they are still in the array as one tho)
var cords = outputwindow.Text.Split(new char[] { 'n' }, StringSplitOptions.RemoveEmptyEntries); // we split the data again and now the result is cords by them selves
foreach (var c in cords)
{
string[] cordstoarray = cords.ToArray(); // cordstoarry should now be the proper data set
//waypoint:name:initials:x:y:z:color:disabled:type:set:rotate_on_tp:tp_yaw:global

decimal startnumb = startingnumberb.Value;

// wc[1] + wc[0] + name + wc[0] + inl + wc[0] + cordstoarray[0] + wc[0] + wc[3] + wc[0] + cordstoarray[1] + wc[0] + wc[4]

for (int i = 0; i < cordstoarray.Length; i++)
{
string[] buildwaypoints = new string[13]; 
string[] wc = { ":", "waypoint", "~", "1:false:0:gui.xaero_default:false:0:false" };
string name = nametextbox.Text; // get the name
string inl = initialstextbox.Text; // get the initials


buildwaypoints[i] = { wc[1] , wc[0] , name , wc[0] , inl , wc[0] , cordstoarray[i] , wc[0] , wc[3] , wc[0] , cordstoarray[i + 1] , wc[0] , wc[4]};  // Build wapypoint array

File.WriteAllLines("Triwaypt.txt", buildwaypoints);
using (StreamReader sr = new StreamReader("Triwaypt.txt"))
{
string res = sr.ReadToEnd();
Console.WriteLine(res);
}
}





}

}
}

您可以使用File.WriteAllLines((,但使用的方式略有不同

首先将List<string> outputLines = new List<string>();添加到程序的开头(就在varlines=…之后(,这样您就可以开始保存生成的行(或者,如果您想保存内存,可以使用File.AppendAllText(

然后,当您生成buildWayPoints数组时,只需生成一个字符串即可添加到列表中。类似的东西

outputLines.Add($"{wc[1]}{wc[0]}{name}{wc[0]}{inl}{wc[0]}{cordstoarray[i]}{wc[0]}{wc[3]}{wc[0]}{cordstoarray[i + 1]}{wc[0]}{wc[4]}");//assuming your code up to this point was correct, also you can replace the {wc[n]} with the literal text if you feel like it

应该可以,但我肯定会建议清理该部分的代码。最后,循环完成后,您需要将新的行列表写入类似File.WriteAllLines(@"Filepath", outputLines)的文件

假设你的问题围绕着创建输出文件,那么上面的问题应该可以完美地解决,如果你的问题实际上是关于正确格式化输入,你需要更清楚地解释你的逻辑,但这是一个黑暗的

counter++;//define counter as int counter = 0; earlier
var twoCords = c.Split(',');
var outputLine = $"waypoint:WLM {counter}:{counter}:{c[0]}:~:{c[1]}:{idk about this one sorry}:true:0:gui.xaero_default:false:0:false";

p.s.我需要提到的一个即时错误是,你做了foreach(cords中的var c(,但你在代码中引用了cords而不是c

最新更新