How to insert data in a new line in excel gotten from C# whenever my .exe file is launched

Viewed 13

I'm having an issue at the moment which I am trying to fix. Trying to concatenate the values(Free space left in my SSD+ time of execution)after each scan I do (the scans are scheduled)and will be saved in a csv file.

`using System; using System.IO;

namespace SSD {

public class Program
{

    static void Main(string[] args)
    {
        DriveInfo[] allDrives = DriveInfo.GetDrives();

        foreach (DriveInfo d in allDrives)
        {

            if (d.IsReady == true)
            {
                var t = d.AvailableFreeSpace;
                var AvailableFree = (t / Math.Pow(10, 9)) - 10;
                var timeOnly = DateTime.Now;

                FileStream ostrm;
                StreamWriter writer;
                TextWriter oldOut = Console.Out;
                try
                {
                    ostrm = new FileStream(@"path.csv", FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new StreamWriter(ostrm);
                    writer.Write(AvailableFree +","+ timeOnly);    

                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot open file.csv for writing");
                    Console.WriteLine(e.Message);
                    return;
                }
                Console.SetOut(writer);
                Console.SetOut(oldOut);
                writer.Close();
                ostrm.Close();
                Console.WriteLine("Done");
           
                    while (true) { }
                }
            }
        }
    }
}

}`

May anyone tell me what I am doing wrong?

Kind regards

0 Answers
Related