Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从txt文件中读取2D数组?C#_C#_Arrays_2d - Fatal编程技术网

如何从txt文件中读取2D数组?C#

如何从txt文件中读取2D数组?C#,c#,arrays,2d,C#,Arrays,2d,这就是我的烂代码 class begin { public static string[] Reader() { string[] theMap = System.IO.File.ReadAllLines(@"C:\Users\Public\Console Slayer\Map\map.txt"); string[] Map = theMap.Clone() as string[];

这就是我的烂代码

    class begin
    {
        public static string[] Reader()
        {
            string[] theMap = System.IO.File.ReadAllLines(@"C:\Users\Public\Console Slayer\Map\map.txt");
            string[] Map = theMap.Clone() as string[];
            return Map;
        }
        public static void Printer()
        {
            foreach (string line in Reader())
            {
                Console.WriteLine(line);
            }

        }
        static void Main()
        {
            Reader();
            Printer();
        }
    }
我想将贴图字符串制作成二维数组,以供要素使用。 我是编程新手,我知道我的代码很糟糕。

尝试使用

Microsoft.VisualBasic.FileIO.TextFieldParser
是的,它是一个VB组件,但它可以工作。不需要重新发明轮子。示例用法:

  OpenFileDialog od = new OpenFileDialog();
        od.Filter = "Tab delimited file (*.txt)|*.txt";
        if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {

            using (var reader = new Microsoft.VisualBasic.FileIO.TextFieldParser(od.FileName))
            {
                reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                reader.Delimiters = new string[] { "\t" }; // the delimeter of the lines in your file


                reader.ReadLine(); // skip header if needed, ignore titles
                while (!reader.EndOfData)
                {
                    try
                    {
                        var currentRow = reader.ReadFields(); // string array
                       // Include code here to handle the row.
                    }

                    catch (Microsoft.VisualBasic.FileIO.MalformedLineException vex)
                    {

                        MessageBox.Show("Line " + vex.Message + " is invalid. Skipping");
                    }
               }
         }
      } 

伟大的你的问题是什么?说真的,我们没有足够的远程信息来帮助您。如何将地图阵列制作成二维阵列您可以提供更多有关您要完成的任务的信息吗?你的数据是什么样子的,为什么你需要一个2D数组,它的结构应该是什么样子的?你不是在帮自己。我们无法猜测您需要什么。字符串已经是一个字符数组。因此,一维字符串数组就是二维字符数组。try块没有右大括号。