-6

probably something INCREDIBLY easy haha but i am having so much trouble trying to figure out the code i would need to write to get the GetArea calculation to be wrote to a file here is some of the code all help appreciated :)

using System;
using System.IO;
namespace system
{
class Rectangle
{

    private double length;
    private double width;

    public void GetDetails()
    {
        Console.WriteLine("Enter Length: ");
        length = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Enter Width: ");
        width = Convert.ToDouble(Console.ReadLine());
    }
    public double GetArea()
    {
        return length * width;
    }
    public void Display()
    {
        Console.WriteLine("Length: {0}", length);
        Console.WriteLine("Width: {0}", width);
        Console.WriteLine("Area: {0}", GetArea()); // this is what i need to be wrote to the file 
    }
    public void WriteToFile()
    {
        //this is the method where i need to find out the code to get the the getarea to write to the file

    }
}


class CalculateRectangle
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        r.GetDetails();
        r.Display();
        r.WriteToFile(); //this is the main method in which i need the area of the rectangle to be wrote to the file
        Console.ReadLine();

    }
}

}

2 Answers2

1

You can use the File class:

File.WriteAllText("filename.txt", GetArea().ToString());

This will write the file in the same folder as your exe, otherwise use a full path like @"C:\filename.txt". If you faced any unauthorized exception, run Visual studio as administrator.

Zein Makki
  • 27,184
  • 5
  • 44
  • 56
0

This will write it to the file location of your choice.

 public void writeToFile(){

 System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", getArea().toString());

 }
Tony Hinkle
  • 4,566
  • 7
  • 20
  • 33
whisk
  • 692
  • 1
  • 10
  • 28