
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication20
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try {
StreamReader Dosya = File.OpenText("dosya.txt"); //Dosyayı açmaya çalış olmaz ise catch bloğuan geç
Dosya.Close();
}
catch {
StreamWriter Dosya = File.CreateText("dosya.txt"); // yeni dosya oluştur.
Dosya.Close();
}
veriGetir();
}
private void buttonEkle_Click(object sender, EventArgs e)
{
if (textBoxAdi.Text == "")
{
MessageBox.Show("hata");
}
else
{
StreamWriter Dosya = File.AppendText("dosya.txt"); //Dosyayı appendText ile yazmak için açtık
Dosya.WriteLine(textBoxAdi.Text); // Dosya.WriteLine ile dosyaya isim ve soyisim text kutularındaki değerleri aralrına bir tab boşluk bırakarak ekledik.
Dosya.Close(); // Dosya yı kapattık.
}
veriGetir();
}
private void buttonListele_Click(object sender, EventArgs e)
{
veriGetir();
}
private void veriGetir() // medot
{
listBoxListe.Items.Clear(); //Listboxda önceden görünen bir değer varsa temizliyoruz
StreamReader dosya = File.OpenText("dosya.txt"); //Stream Reader ile Dosyayı okuyoruz.
string s = dosya.ReadLine();
while (s != null)
{
listBoxListe.Items.Add(s);
s = dosya.ReadLine();
}
dosya.Close();
}
private void buttonAra_Click(object sender, EventArgs e)
{
string aran = textBoxAra.Text;
labelSonucAra.Text = ara(aran);
}
//***************************************************
string sonuc = "";
public String ara(String aranan)
{
StreamReader oku = new StreamReader("dosya.txt"); // okumak için dosyamızı açıyor
while (!oku.EndOfStream) // while döngüsü yapıyoruz
{
if (oku.ReadLine() == aranan) // eğer okunan eşitse arana kelimeye
{ sonuc= aranan; //aranan kelimeyi döndür
break; // aranan kelime bulundu ve while döngüsünden çık
}
else
{ sonuc= "bulunamadı"; } //bulunamadı ise bulunamadı olarak döndür
}
return sonuc;
}
}
}
</pre>
Hiç yorum yok:
Yorum Gönder