0
        private void btn_Olustur_Click(object sender, EventArgs e) //dosya oluştur click
    {
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\yenidosya.txt";
        StreamWriter dosya = File.CreateText(path);
        if (!File.Exists(path))
        {
            //StreamWriter dosya = File.CreateText(path);
            for (int i = 0; i < liste.Count; i++)
            {
                dosya.WriteLine(liste.ElementAt(i));
            }
            dosya.Close();
            MessageBox.Show("Dosya oluşturuldu.", "BASARILI!");
        }
        else
        {
            DialogResult dialogResult = MessageBox.Show("Aynı isimde dosya mevcuttur(yenidosya.txt), değiştirilsin mi?", "HATA!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            //MessageBox.Show("Aynı isimde dosya mevcuttur. (yenidosya.txt)", "HATA!");
            if (dialogResult == DialogResult.Yes)
            {
                File.Delete(path);

                for (int i = 0; i < liste.Count; i++)
                {
                    dosya.WriteLine(liste.ElementAt(i));
                }
                dosya.Close();
                MessageBox.Show("Dosya oluşturuldu.", "BASARILI!");
            }
        }
    }

If the file exists I want to delete it. But it gives a thread fault. error string: File.Delete (path);

error message: The process cannot access file "path", because it is being used by another process.

  • Please improve your question following the guidance of [ask]. It is lacking an actual question. What you have now is merely a description of the Problem. What have you tried? Why did it fail? What do you want to happen in that case? – Fildor Sep 15 '17 at 06:54
  • 1
    You create the file but you do not close the stream before deleting. See linked dupe, there is a pretty similar example. That said thus code doesn't make much sense, what are you trying to do? – Adriano Repetti Sep 15 '17 at 06:55
  • I want to delete the txt file in the yes option, but it gives an error. –  Sep 15 '17 at 06:56
  • You can't delete it because you keep it open with the previous call to `CreateText()`. However...Why don't you first ask and then create the file?! Also check `File.WriteAllLines()`, it's much more handy. – Adriano Repetti Sep 15 '17 at 07:08
  • okey, thanks, My mistake is not creating the file again after the file is deleted. –  Sep 15 '17 at 07:29

0 Answers0