Yaz  Font K   lt Yaz  Font B y lt

Text Dosya İçindeki Metini Silmek

 

Merhaba arkadaşlar bu makalemizde mevcut text dosyasındaki metini sileceğiz. StreamReader ile text dosyamızı okuyoruz, StreamWriter ile text dosyaya yazma işlemini yapabiliyoruz.

 

 

Resim1

Şekil 1

 

Resim2

Şekil 2

 

Resim3

Şekil 3

 

Form1.cs

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using static System.Net.Mime.MediaTypeNames;

 

namespace text_clear

{

    public partial class Form1 : Form

    {

        public Form1()

        {

             InitializeComponent();

        }

 

        private void btnDelete_Click(object sender, EventArgs e)

        {

            

            string path = "D:\\Read Me.txt";              

            string str;

 

            //here the text file is read

            //burada text dosyasi okunur

            StreamReader sr = new StreamReader(path);

            str = sr.ReadToEnd();

            sr.Close();

 

            //if the text file is not empty

            //text dosyasi icerigi bos degilse

            if (str != "")

 

            {   

                //bu kisimda text dosya icerigi silinir

                //in this section, the text file content is deleted

                TextWriter tw = new StreamWriter(path);

                tw.Write("");

                tw.Close();

                 MessageBox.Show("The content of the text file has been deleted. " + Environment.NewLine + " Text dosyasi icerigi silindi. ""Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

 

            {

               

                 MessageBox.Show("Text file content is already empty." + Environment.NewLine + " Text dosyasi icerigi zaten bos. ""Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

 

            }

           

        }

    }

}

      

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN