using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace IFactory
{
    class Test
    {
        static int Main(string[] args)
        {
            if (args.Length != 3) {
                Console.WriteLine("usage: Test <filename> <field name> <record number>");
                return 1;
            }            StringBuilder buf = new StringBuilder();
            using (FileStream fs = File.OpenRead(args[0])) 
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0) 
                {
                    buf.Append(temp.GetString(b));
                }
            }            string fileContents = buf.ToString();
            Console.Write(fileContents);
            CSV csv = new CSV (fileContents);
            string fieldName = args[1];            int recnum = int.Parse(args[2]);            Console.WriteLine(csv.csvget(fieldName, recnum));
            return 0;
        }
        
    }
    public class CSV {
        public CSV (String fileContents) {        }    
        public string csvget (String fieldName, int recnum) {
            return "filedName";        }    }}
       

解决方案 »

  1.   

    以前写的一个类,你参考下吧
    using System;
    using System.IO;
    using System.Text;
    using System.Data;
    using System.Collections;namespace CSVOperator
    {
    public class CCSVRead
    {
    private System.IO.FileStream MyFileStream;
    private System.IO.StreamReader ReadStream;
    public CCSVRead()
    {
                
    }
    public bool Eof
    {
    get
    {
                    if (ReadStream != null)
                        return ReadStream.Peek() == -1;
                    else
                        return true;
    }
    } public bool OpenFile(string aFileName)
    {
    try
    {
    MyFileStream = new FileStream(aFileName, System.IO.FileMode.Open);
    ReadStream = new StreamReader(MyFileStream,System.Text.Encoding.Default);
    }
    catch
    {
    return false;
    }
    return true;
    } public void CloseFile()
    {
    ReadStream.Close();
    MyFileStream = null;
    } public string[] MyReadLine()
    {
    StringBuilder sb = new StringBuilder();
    string [] returnstrings;
    char buffer;

    while(ReadStream.Peek() >= 0)
    {
    buffer = (char)ReadStream.Read();

    if(buffer.Equals('\r'))
    {
    if(((char)ReadStream.Peek()).Equals('\n'))
    {
    ReadStream.Read();
    break;
    }
    }
    else
    {
    sb.Append(buffer);
    }
    } if(sb.Length > 0)
    {
    returnstrings = SplitStr(sb.ToString());
    return returnstrings;
    }
    return null;
    } public DataTable GetAllData()
    {
    DataTable OutputTable = new DataTable();
    string[] CellsStr;
    Int64 Lineindex=0; int ColumnLen = 0;
    while(((CellsStr = MyReadLine()) != null)|| ReadStream.Peek() >= 0)
    {
    Lineindex ++;
    try
    {
    int Len, i;
    Len = CellsStr.Length;
    if(Len > ColumnLen)
    {
    for(i = ColumnLen; i < Len; i ++)
    {
    if(Lineindex == 1)
    {
    OutputTable.Columns.Add(CellsStr[i]);
    }
    else
    {
    OutputTable.Columns.Add("Column" + (i + 1).ToString());
    }
    ColumnLen ++;
    }
    }
    if(Lineindex > 1)
     OutputTable.Rows.Add(CellsStr);
    }
    catch
    {}
    }
    return OutputTable;
    } private string[] SplitStr(string aStr)
    {
    int i;
    int acount=0;
    int StartPos=0;
        ArrayList SplitList = new ArrayList();
    //Split string with ','
    for(i=0; i < aStr.Length; i++)
    {
    if(aStr[i] == '\"')
    {
    acount ++;
    }
    else if(aStr[i] == ',')
    {
    if((acount%2)==0)
    {
    SplitList.Add(aStr.Substring(StartPos, i-StartPos));
    StartPos = i + 1;
    acount = 0;
    }
    }                //The end of string
    if(i == aStr.Length-1)
    {
    SplitList.Add(aStr.Substring(StartPos, i-StartPos + 1));
    }
    } //Create a output string array
    string[] OutputStrs;
    OutputStrs = new string[SplitList.Count];
    //Put the arraylist values into string array
    for(i=0; i<SplitList.Count; i++)
    {
    string temp;
    temp = SplitList[i].ToString();
    //Cut the '"' at the start and the end postion for string
    if (temp.StartsWith("\""))
    {
    temp = temp.Substring(1);
    }
    if (temp.EndsWith("\""))
    {
    temp = temp.Substring(0, temp.Length - 1);
    }
    OutputStrs[i] = temp.Replace("\"\"","\"");
    }
    return OutputStrs;
    }
    }
    }