how to save array in file in c#

本文介绍如何使用 C# 将单维或二维字符串数组保存到文本文件,并提供示例代码来读取文件内容。对于二维数组,演示了如何将其转换为单行字符串并保存。
  • Question

  • hi all,

    i have single table and i want to save it in file 

    EX:
    1  a  b  c   d
    2  q  r   t   j  
    3  w  j   s  r

    thanks for help
    Wednesday, March 17, 2010 4:28 PM
    Avatar of Ahmed_Soft3
    IS
    95 Points

Answers

  • I am assuming its a single dimension string array. 

    Use the following

    string[] table = { "1 a b c d", "2 q r t j", "3 w j s r" };
    System.IO.File.WriteAllLines("c:\\test.txt", table);
    
    If its in a 2d array, then use the following code

                string[,] dataTable = new string[3,4];
                
                ///Fill the table
    
                List<string> linesToWrite = new List<string>();
                for(int rowIndex = 0; rowIndex < 3; rowIndex++)
                {
                    StringBuilder line = new StringBuilder();
                    for(int colIndex = 0; colIndex < 4; colIndex++)
                        line.Append(dataTable[rowIndex, colIndex]).Append(" ");
                    linesToWrite.Add(line.ToString());
                }
    
                System.IO.File.WriteAllLines("C:\\test.txt", linesToWrite.ToArray());



    Thanks,
    A.m.a.L 
    Dot Net Goodies 
    Don't hate the hacker, hate the code
    • Edited by A.m.a.L HashimMVP Wednesday, March 17, 2010 4:42 PM more code
    • Marked as answer by Liliane Teng Wednesday, March 24, 2010 10:24 AM
    Wednesday, March 17, 2010 4:35 PM
    Avatar of A.m.a.L Hashim
    Independent Expert
    (Partner, MVP)
    39,971 Points
  • I have put array in hashtable by make value (array) Myhashtable.Add("MyKay", new string[0]);
    • Marked as answer by Ahmed_Soft3 Wednesday, March 24, 2010 12:38 PM
    Wednesday, March 24, 2010 12:38 PM
    Avatar of Ahmed_Soft3
    IS
    95 Points
  • Hello Ahmed,
    Welcome to MSDN Form.
    You can use StreamReader class to realize.Here is an simple example.I hope this could help you.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace CA1
    {
        class Program
        {
            static void Main(string[] args)
            {
               string[] table = { "1 a b c d","2 q r t j","3 w j s r" };
               System.IO.File.WriteAllLines("c:\\test.txt", table);
              
               StreamReader reader = new StreamReader("c:\\test.txt", System.Text.Encoding.Default);
               string[] values = reader.ReadToEnd().Split('\n');
               for (int i = 0; i <3; i++)
               {
                     System .Console.WriteLine(values[i]);
               } 
     
            }
        }
    }
    Best regards!
    Liliane Teng


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, March 24, 2010 3:33 PM
    Wednesday, March 24, 2010 12:57 PM
    Avatar of Liliane Teng
    7,415 Points

All replies

  • I am assuming its a single dimension string array. 

    Use the following

    string[] table = { "1 a b c d", "2 q r t j", "3 w j s r" };
    System.IO.File.WriteAllLines("c:\\test.txt", table);
    
    If its in a 2d array, then use the following code

                string[,] dataTable = new string[3,4];
                
                ///Fill the table
    
                List<string> linesToWrite = new List<string>();
                for(int rowIndex = 0; rowIndex < 3; rowIndex++)
                {
                    StringBuilder line = new StringBuilder();
                    for(int colIndex = 0; colIndex < 4; colIndex++)
                        line.Append(dataTable[rowIndex, colIndex]).Append(" ");
                    linesToWrite.Add(line.ToString());
                }
    
                System.IO.File.WriteAllLines("C:\\test.txt", linesToWrite.ToArray());



    Thanks,
    A.m.a.L 
    Dot Net Goodies 
    Don't hate the hacker, hate the code
    • Edited by A.m.a.L HashimMVP Wednesday, March 17, 2010 4:42 PM more code
    • Marked as answer by Liliane Teng Wednesday, March 24, 2010 10:24 AM
    Wednesday, March 17, 2010 4:35 PM
    Avatar of A.m.a.L Hashim
    Independent Expert
    (Partner, MVP)
    39,971 Points
  • thanks  A.m.a.L - aditi.com - Think Product Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals

    but how i can call data to array after saving in file

    can we make somthing like hashtable but hashtable contain one value (Key - Value) and i have more than value
    Wednesday, March 17, 2010 11:11 PM
    Avatar of Ahmed_Soft3
    IS
    95 Points
  • Seems you are looking for one key with many values.

    In codeplex one library is available which can help you

    http://www.codeplex.com/PowerCollections


    Thanks,
    A.m.a.L
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Wednesday, March 24, 2010 12:25 PM
    Avatar of A.m.a.L Hashim
    Independent Expert
    (Partner, MVP)
    39,971 Points
  • I have put array in hashtable by make value (array) Myhashtable.Add("MyKay", new string[0]);
    • Marked as answer by Ahmed_Soft3 Wednesday, March 24, 2010 12:38 PM
    Wednesday, March 24, 2010 12:38 PM
    Avatar of Ahmed_Soft3
    IS
    95 Points
  • Hello Ahmed,
    Welcome to MSDN Form.
    You can use StreamReader class to realize.Here is an simple example.I hope this could help you.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace CA1
    {
        class Program
        {
            static void Main(string[] args)
            {
               string[] table = { "1 a b c d","2 q r t j","3 w j s r" };
               System.IO.File.WriteAllLines("c:\\test.txt", table);
              
               StreamReader reader = new StreamReader("c:\\test.txt", System.Text.Encoding.Default);
               string[] values = reader.ReadToEnd().Split('\n');
               for (int i = 0; i <3; i++)
               {
                     System .Console.WriteLine(values[i]);
               } 
     
            }
        }
    }
    Best regards!
    Liliane Teng


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Wednesday, March 24, 2010 3:33 PM
    Wednesday, March 24, 2010 12:57 PM
    Avatar of Liliane Teng
    7,415 Points
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值