File in use when no program is using it.

  • Thread starter Thread starter Griffin G
  • Start date Start date
G

Griffin G

Guest
I am trying to get the user to select a image from a folder, and then be able to clear all files in that folder the file resides in. I get the error System.IO.IOException: 'The process cannot access the file '0001.png' because it is being used by another process.' My program doesn't even open the file, and if it does it removes it from the viewing picturebox. Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;
using System.IO;
namespace DiscordCacheReader
{
public partial class Form1 : Form
{
Image file;
public Form1()
{


InitializeComponent();
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.ImageLocation = @"https://proxy.duckduckgo.com/iu/?u=...nt/uploads/2017/12/No_Image_Available.jpg&f=1";
}

private void label1_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("Error! Please provide a path", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
pictureBox1.ImageLocation = @textBox1.Text;
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
button3.Visible = true;
}

}

private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\discord\Cache";
choofdlog.Filter = "File (*.*)|*.*";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = false;


if (choofdlog.ShowDialog() == DialogResult.OK)
{
var sFileName = choofdlog.FileName;
textBox1.Text = sFileName;
file = Image.FromFile(choofdlog.FileName);
button1.Visible = true;
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog f = new SaveFileDialog();
f.Filter = "PNG (*.png)|*.png";
f.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (f.ShowDialog() == DialogResult.OK)
{

file.Save(f.FileName);
}
}

private void button4_Click(object sender, EventArgs e)
{
if (Process.GetProcessesByName("discord").Length > 0)
{
DialogResult d1 = MessageBox.Show("Error! Discord is running! Click retry to close discord and retry!", "Error!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

if (d1 == DialogResult.Retry)
{
Process[] localByName = Process.GetProcessesByName("discord");
foreach (Process p in localByName)
{
p.Kill();
}

button4_Click(sender, e);
}
}
else
{
if (textBox1.Text.Length != 0)
{
pictureBox1.ImageLocation = @"https://proxy.duckduckgo.com/iu/?u=...nt/uploads/2017/12/No_Image_Available.jpg&f=1";
string parent = System.IO.Directory.GetParent(@textBox1.Text).FullName;

int fileCount = Directory.GetFiles(parent).Length;
DialogResult d2 = MessageBox.Show("Discord is closed! Are you sure you want to clear your cached folder? This will delete " + fileCount + @" file(s) and may not be recoverable!", "Info!", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (d2 == DialogResult.No)
{
MessageBox.Show("Canceled!", "Info!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
pictureBox1.ImageLocation = @"https://proxy.duckduckgo.com/iu/?u=...nt/uploads/2017/12/No_Image_Available.jpg&f=1";



string epice = System.IO.Directory.GetParent(textBox1.Text).FullName;
System.IO.DirectoryInfo di = new DirectoryInfo(epice);

foreach (FileInfo file in di.GetFiles())
{

string finalpath = @di + "\\" + @file;

MessageBox.Show(finalpath);

using (StreamReader sr = new StreamReader(@finalpath))
{
file.Delete();
}

}
}
}
else
{
MessageBox.Show("Error! Please provide a path", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}







Griffin

Continue reading...
 
Back
Top