Hello,
-The error I get is an exception "cannot write in a closed file"
-The function has an object argument because WriteLine() has an overload that receives an object argument, string, int...as long as the object overrides ToString() it works okay.
But that's not my problem I'd like to know how to position the codes in my program. The same code works in Console application !
It's my first time to use this forum I don't know if sending codes is okay. In case it's not - Sorry :)
Here is the code in console:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace StreamReadWrite
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Name = "jeannette";
p.Id = 666;
Person p1 = new Person();
p1.Name = "Jonathan";
p1.Id = 43443;
FileInfo f = new FileInfo("ReadWriteTest.txt");
StreamWriter Swriter = f.CreateText();
Swriter.WriteLine(" mama is my witness");
Swriter.WriteLine(p);
Swriter.WriteLine(p1);
Swriter.WriteLine(" I love programming in c#");
Swriter.Write(Swriter.NewLine);
Swriter.Close();
Console.WriteLine("The file ReadWriteTest has been created ");
StreamReader Sreader = File.OpenText("ReadWriteTest.txt");
string input = "";
while ((input = Sreader.ReadLine()) != null)
{
Console.WriteLine(input.ToString());
}
Sreader.Close();
Console.Read();
}
public class Person
{
public string Name { get; set; }
public int Id { get; set; }
public override string ToString()
{
return " Name : "+Name+" "+"Id :"+Id;
}
}
}
}