-1

I want to get all the files name and their size and save it into an Excel using C#. I have done this but the problem is I want two different column: one for filename and one for Filesize, but my output is saved in one column.

The code I'm using:

public void Main()
{
    using (StreamWriter writer = new StreamWriter("D:\\MyFile\\output.csv"))
    {
        Console.SetOut(writer);
        Act();
    }
}

static void Act()
{
    Console.Write("FileName,".PadRight(20));
    Console.WriteLine("Size");
    DirectoryInfo di = new DirectoryInfo(@"D:\MyFile");
    // Get a reference to each file in that directory.
    DataSet ds = new DataSet();
    FileInfo[] fiArr = di.GetFiles();
    // Display the names and sizes of the files.
    //Console.WriteLine("The directory {0} contains the following files:", di.Name);
    foreach (FileInfo f in fiArr)
        Console.WriteLine("{0}, {1} ", f.Name.PadRight(20), f.Length.ToString());
}

How can I split this into two columns?

kayess
  • 3,384
  • 9
  • 28
  • 45
SaNa3819
  • 337
  • 5
  • 19

1 Answers1

0

Try seperating the fields with a tab.

foreach (FileInfo f in fiArr)
    Console.WriteLine("{0}\t {1} ", f.Name.PadRight(20), f.Length.ToString());

Good luck my friend.

Asaf Savich
  • 623
  • 1
  • 9
  • 29
  • Have you tried using new line? `Console.WriteLine("{0}{1} {2} ", f.Name.PadRight(20), "\n" ,f.Length.ToString());` – Asaf Savich Mar 24 '16 at 08:14
  • yes for new line it goes to a new line but i want the next column – SaNa3819 Mar 24 '16 at 08:16
  • Haha now i think i've found it. `Console.WriteLine("{0}{1}{2} ", f.Name.PadRight(20), ";" ,f.Length.ToString());` – Asaf Savich Mar 24 '16 at 08:19
  • No its not working It gives the result under A column a.txt ;19039 b.txt ;734 – SaNa3819 Mar 24 '16 at 08:24
  • weird. Anyway, i've looked it up and found [this](http://stackoverflow.com/questions/24404677/how-do-i-write-data-column-b-y-column-to-a-csv-file). They also suggest using **;**. – Asaf Savich Mar 24 '16 at 08:47