StreamReader sr = new StreamReader(fs);
string s = sr.ReadLine();
while (s != null)
{
// do something with the word s
s = sr.ReadLine()
}
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace KundReg
{
/// <summary>
/// Summary description for Openfile.
/// </summary>
public class Openfile
{
public Openfile()
{
fileName = "customer.dat";
}
protected string fileName;
public string FileName
{
get
{
return fileName;
}
set
{
fileName = value;
}
}
public ArrayList getCustomer()
{
Customer customer;
ArrayList alCustomer = new ArrayList();
FileStream fil = new FileStream(fileName, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
while(true)
{
customer = (Customer)bf.Deserialize(fil);
alCustomer.Add(customer);
}
fil.Close();
Console.WriteLine(alCustomer.Count);
return alCustomer;
}
public void newCustomer(Customer customer)
{
FileStream fil = new FileStream(fileName, FileMode.Append);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fil, customer);
fil.Close();
}
}
}
while(true)
{
customer = (Customer)bf.Deserialize(fil);
alCustomer.Add(customer);
}
IFormatter f = new BinaryFormatter();
Stream s = new FileStream("C:\file.dat", FileMode.Create, FileAccess.Write, FileShare.None);
f.Serialize(s, alCustomer);
s.Close();
[Serializable]
public struct Customer
{
public string firstname;
public string lastname;
public string address;
public string email;
public int userID;
}
public ArrayList getCustomer()
{
Customer customer;
ArrayList alCustomer = new ArrayList();
FileStream fil = new FileStream(fileName, FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
try
{
while(true)
{
customer = (Customer)bf.Deserialize(fil);
alCustomer.Add(customer);
}
}
catch(Exception e)
{
}
finally
{
fil.Close();
}
return alCustomer;
}