'When creating and deleting files when it's deleting the files it's throwing exception the file is in use by another process. how to solve?
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;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Generate_Files
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void GenerateImages()
{
Random rand = new Random();
System.Drawing.Font drawfont = new System.Drawing.Font("Arial", 16);
for (int I = 0; I < 10; I++)
{
System.Drawing.Bitmap B = new System.Drawing.Bitmap(rand.Next(1, 500), rand.Next(1, 500));
System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(B);
G.DrawString(I.ToString(), drawfont, System.Drawing.Brushes.Black, new System.Drawing.PointF(100.0f, 100.0f));
B.Save(@"d:\Images\" + I + ".png");
B.Dispose();
}
Thread.Sleep(5000);
DeleteFiles();
}
private void DeleteFiles()
{
System.IO.DirectoryInfo di = new DirectoryInfo(@"d:\Images");
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
Thread.Sleep(5000);
GenerateRanomUniqueFilesNames();
}
private void GenerateRanomUniqueFilesNames()
{
for (int i = 0; i < 10; i++)
{
string myUniqueFileName = $@"{Guid.NewGuid()}.txt";
File.Create(@"d:\Images\" + myUniqueFileName);
}
Thread.Sleep(5000);
DeleteFiles();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
GenerateRanomUniqueFilesNames();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
}
}
At this time i'm text files with unique names to each file waiting 5 seconds deleting the files and then creating again new ones then deleting and again.
The problem is once it's trying to delete the first file i'm getting the exception.
The exception is on this line :
file.Delete();
System.IO.IOException: 'The process cannot access the file '284df30d-4624-4603-86a9-0d63e746d439.txt' because it is being used by another process.'
Maybe i need some how to dispose each file after creating him in the GenerateRanomUniqueFilesNames method ?
When i used the GenerateImages() method i didn't have this exception but with the txt files the problem occurred.
Solution 1:[1]
The docs about File.Create say:
Returns FileStream
A FileStream that provides read/write access to the file specified in path.
This means that the file is open when you try to delete it. To make your code work without exceptions then you need a way to immediately close the stream returned by File.Create.
One simple way could be:
for (int i = 0; i < 10; i++)
{
string myUniqueFileName = $@"{Guid.NewGuid()}.txt";
using(File.Create(@"d:\Images\" + myUniqueFileName))
{ ; }
}
The using statement will call Dispose on the FileStream returned and the files created are now closed and available for deletion.
Some test on my machine shows that also a line like this will work
File.Create(@"e:\temp\temporary.txt").Close();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
