C#将.lst文件转换为Array

时间:2015-04-13 07:36:48

标签: c# arrays performance list

我有一个.lst文件。看起来像这样。但我认为有一种更好,更快,更简单的方法来导入.lst文件。因为excel可以导入它,所以也许它可能在c#中。

如何在c#中获取.lst文件到数组?看起来像excel表

* DXF-Dateiname Funktionsnummer Blattnummer Benennung

DXFO0001.dxf 114D197 DECK 01 DECKBLATT
 DXFO0002.dxf 114D197 G72 01
 DXFO0003.dxf 114I197 BL 01
 DXFO0004.dxf 114I197 BL 02 *

通常文件更大我只是发布这个小部分进行测试。

我可以将文件导入excel。

excel image with date from above

所以我想我可以用c#将.lst文件导入数组。我知道如何从微软的这个函数中获取c#文件。

    using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor 
{
    public static void Main(string[] args) 
    {
        foreach(string path in args) 
        {
            if(File.Exists(path)) 
            {
                // This path is a file
                ProcessFile(path); 
            }               
            else if(Directory.Exists(path)) 
            {
            // This path is a directory
            ProcessDirectory(path);
        }
        else 
        {
            Console.WriteLine("{0} is not a valid file or directory.", path);
        }        
    }        
}


// Process all files in the directory passed in, recurse on any directories 
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory) 
{
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(targetDirectory);
    foreach(string fileName in fileEntries)
        ProcessFile(fileName);

    // Recurse into subdirectories of this directory.
    string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
    foreach(string subdirectory in subdirectoryEntries)
        ProcessDirectory(subdirectory);
}

// Insert logic for processing found files here.
public static void ProcessFile(string path) 
{
    Console.WriteLine("Processed file '{0}'.", path);       
}

}

2 个答案:

答案 0 :(得分:1)

这样的事情:

public string[][] ProcessFile(string fileName)
{
    var lines = File.ReadAllText(fileName).Split(new [] { Environment.NewLine }, 
        StringSplitOptions.RemoveEmptyEntries);

    // skip first line
    return lines.Skip(1).Select(line => line.Split(new[] {" "}, 
        StringSplitOptions.RemoveEmptyEntries)).ToArray();
}

答案 1 :(得分:0)

我相信您正在寻找CSV到数组代码? Here可能是您需要的东西。