将String Array从文本文件转换为struct中的整数

时间:2018-01-28 22:42:07

标签: c#

我正在尝试编写一个程序,我有一个省,它有很多属性。当我启动程序时,它应读取textFile(provinceData.txt)中的所有行,将其放入Array中,然后将该值放入struct中。现在我尝试了所有我知道的转换变量的方法,但我还没有找到解决方案。当我启动程序时,Visual Studio说"输入字符串格式错误"。

以下是我已经获得的代码:

 public struct provinceStruct001
 {
     public string name;
     public int terrain;
 }

 static void getProvinceDataFromFile()
 {
     string[] allLines = File.ReadAllLines("../../save/provinceData.txt");
     //puts all the Lines in the Text File into an Array

     provinceStruct001 ProvinceStruct001 = new provinceStruct001();

     ProvinceStruct001.name = allLines[4 + 1];

     ProvinceStruct001.terrain=Convert.ToInt32(Convert.ToString(allLines[8 + 1]));

     //8 is the Line in the textFile and +1 because an array starts at 0, 
     and i cant read line 0 in a text file
}

*

这是TextFile

   struct provinceStruct001
   {

   string name;

   Munich  //this

   int occupier;

   0       //this 0=noone 1=rebels 2=foreign_country

   int terrain;

   0       //and this are the only important lines 
            0=flatlands,1=forest,2=hilly,3=mountainous

   bool isCapital;

  false

我感谢大家,他们花时间提前帮助我。

0 个答案:

没有答案