-2

I research all related title/topics to this problem but none of it seems to solve my problem.. it produces the 'Seperated_File.txt' but it is totally a blank text file. Thanks for your reply.

UPDATED (THE SOLUTION)

        string[] lines = File.ReadAllLines(@"d:\Processed_Text.txt");
        DataTable dt = new DataTable();

        dt.Columns.Add("IP Address");
        dt.Columns.Add("Date");
        dt.Columns.Add("Time");
        dt.Columns.Add("GMT");
        dt.Columns.Add("Method");
        dt.Columns.Add("Resource");
        dt.Columns.Add("Port");
        dt.Columns.Add("Bytes");
        dt.Columns.Add("Services");
        dt.Columns.Add("Destination");
        dt.Columns.Add("Operating System");

        foreach (string line in lines)
        {  
            string[] split = line.Split(' ', ':');


            split = split.Where(r => !string.IsNullOrWhiteSpace(r)).ToArray();

            Foo foo = new Foo();

            foo.IPAddress = split[0];
            foo.Date = split[1];
            foo.Time = string.Format("{0}:{1}:{2}", split[2], split[3], split[4]);
            foo.GMT = split[5];
            foo.Method = split[6];
            foo.Resource = split[7];
            foo.Port = split[8];
            foo.Bytes = split[9];
            foo.Service = split[10];
            foo.Destination = split[11];
            foo.OS = split[12];

            dt.Rows.Add(foo.IPAddress, foo.Date, foo.Time, foo.GMT, foo.Method, foo.Resource, foo.Port, foo.Bytes, foo.Service, foo.Destination, foo.OS);
        }

          using (StreamWriter sw = new StreamWriter(@"d:\Seperated_Text.txt"))
          foreach (DataRow dr in dt.Rows)
         {
            Console.WriteLine(string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}", dr[0], dr[1], dr[2], dr[3], dr[4], dr[5], dr[6], dr[7], dr[8], dr[9], dr[10]));
            sw.WriteLine(String.Join(",", dr.ItemArray));
            sw.WriteLine(dr);

        }
        Console.ReadLine();
    }
1aliff
  • 435
  • 2
  • 5
  • 15
  • 1
    Please post your actual code. You've got `result` mixed with `vresult` and you mention `sw`, but it looks like you actually mean `objWriter`. – itsme86 Jun 12 '17 at 03:33
  • 1
    edited, sorry for inconvenience. Thought i wanna show what ive tried. – 1aliff Jun 12 '17 at 03:41
  • Updated: my code is the solution.. got it by myself. Thanks for reply. – 1aliff Jun 12 '17 at 04:40

2 Answers2

0

In order to access the data inside a DataRow, you need to do:

foreach (DataRow row in table.Rows)
            {
                Console.WriteLine($"{row.ItemArray[0]} | {row.ItemArray[1]} | {row.ItemArray[2]}");
            }

or if you prefer:

foreach (DataRow row in table.Rows)
    {
        Console.WriteLine($"{row["IP Address"]} | {row["Date"]} | {row["Time"]}");
    }
gilliduck
  • 2,762
  • 2
  • 16
  • 32
  • then, from here i can just directly save it as .txt file is it? – 1aliff Jun 12 '17 at 04:17
  • Well you'll need to use a filewriter, but yeah. You're issue appears mostly to be that you weren't calling any visible data, so when you were writing the .txt file there was nothing to write. – gilliduck Jun 12 '17 at 04:18
  • i fixed it using streamwriter... thank you for your reply anyway :) – 1aliff Jun 12 '17 at 04:38
0

The reason the file is blank is because it already exists and you are not appending using the 2nd argument of StreamWriter:

bool append = true;
using (StreamWriter sw = new StreamWriter(@"d:\Seperated_Text.txt", append)) 
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321