例子:Read content from file to Arraylist
--------------Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;
using System.Configuration;
namespace Test
{
class Program
{
static void Main(string[] args)
{
ReadFile rf = new ReadFile();
string[,] str = null;
str=rf.ReadFileToArray();
Console.ReadKey();
}
}
}
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;
using System.Configuration;
namespace Test
{
class Program
{
static void Main(string[] args)
{
ReadFile rf = new ReadFile();
string[,] str = null;
str=rf.ReadFileToArray();
Console.ReadKey();
}
}
}
--------------ReadFile.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;
using System.Configuration;
namespace Test
{
class ReadFile
{
public ArrayList getFileContent()
{
ArrayList AL = new ArrayList();
string strFilePath = System.Environment.CurrentDirectory + @"\" + ConfigurationSettings.AppSettings["FileName"];
if (!File.Exists(strFilePath))
{
Console.WriteLine("File is not there");
}
try
{
StreamReader sr = new StreamReader(strFilePath, Encoding.GetEncoding("gb2312"));
string l;
while ((l = sr.ReadLine()) != null)
{
AL.Add(l);
}
sr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return AL;
}
public string[,] ReadFileToArray()
{
string[,] iret = null;
ArrayList alNumLine = getFileContent();
string[] strLineArr = null;
if (alNumLine.Count > 0)
{
strLineArr = Convert.ToString(alNumLine[0]).Trim(',').Split(',');
iret = new string[alNumLine.Count, strLineArr.Length];
for (int i = 0; i < alNumLine.Count; i++)
{
strLineArr = Convert.ToString(alNumLine[i]).Trim(',').Split(',');
for (int j = 0; j < strLineArr.Length; j++)
{
iret[i, j] = Convert.ToString(strLineArr[j]);
}
}
}
return iret;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;
using System.Configuration;
namespace Test
{
class ReadFile
{
public ArrayList getFileContent()
{
ArrayList AL = new ArrayList();
string strFilePath = System.Environment.CurrentDirectory + @"\" + ConfigurationSettings.AppSettings["FileName"];
if (!File.Exists(strFilePath))
{
Console.WriteLine("File is not there");
}
try
{
StreamReader sr = new StreamReader(strFilePath, Encoding.GetEncoding("gb2312"));
string l;
while ((l = sr.ReadLine()) != null)
{
AL.Add(l);
}
sr.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return AL;
}
public string[,] ReadFileToArray()
{
string[,] iret = null;
ArrayList alNumLine = getFileContent();
string[] strLineArr = null;
if (alNumLine.Count > 0)
{
strLineArr = Convert.ToString(alNumLine[0]).Trim(',').Split(',');
iret = new string[alNumLine.Count, strLineArr.Length];
for (int i = 0; i < alNumLine.Count; i++)
{
strLineArr = Convert.ToString(alNumLine[i]).Trim(',').Split(',');
for (int j = 0; j < strLineArr.Length; j++)
{
iret[i, j] = Convert.ToString(strLineArr[j]);
}
}
}
return iret;
}
}
}
--------------App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<clear/>
<add key="FileName" value="a.txt"/>
</appSettings>
</configuration>
<configuration>
<appSettings>
<clear/>
<add key="FileName" value="a.txt"/>
</appSettings>
</configuration>
路在脚下