0

I have a .lst file. Which looks like this. But I thougt there is a better, a faster and easier way to import the .lst file. Because excel can import it, so maybe its possible in c#, too.

How do I get a .lst file to an Array in c# ? Which looks like the excel table

*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*

Normally the file is way bigger i just posted this little part for testing.

I can import the file to excel.

excel image with date from above

So I thougt I can import the .lst file with c# to an array. I know how to get file in c# like this function from microsoft.

    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);       
}

}

  • 3
    The C# cod eyou pasted is about listing directories, not about parsing and .lst file (which looks like a CSV file to me) What is your question exactly? – Emond Apr 13 '15 at 07:40
  • @ErnodeWeerd How do I get a .lst file to an Array in c# ? Whicht looks like the excel table. –  Apr 13 '15 at 07:41
  • Can you open the file with notepad a share a part of it? – aloisdg Apr 13 '15 at 07:45
  • @aloisdg The data of the file is _italic_ –  Apr 13 '15 at 07:47

2 Answers2

1

Something like this:

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();
}
Igor Semin
  • 2,486
  • 1
  • 20
  • 21
  • This is good but just a one dimenstional Array I need a two dimensional Array string[][] (like the excel sheet ) –  Apr 13 '15 at 07:55
  • that i just brilliant and working in my code. Thank you so much –  Apr 13 '15 at 08:06
0

I believe you are looking for a CSV to Array code? Here is something that might be what you need.

Community
  • 1
  • 1
Junaid
  • 1,708
  • 16
  • 25