Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting File which is displayed in picturebox
    primarykey
    data
    text
    <p>I am selecting file from openfiledialoge and displaying it in picturebox and its name in textbox when I click on <code>delete</code> button I am getting exception <code>The process cannot access the file because it is being used by another process.</code> I searched a lot for this exception to get resolved but i didn't fine any of them working, when i tried closing file with imagename which is in textbox i.e the file i am displaying in picturebox ; using <code>IsFileLocked</code> method,this closes and deletes all files of particular directory path ,but how can I delete the only file shown in picturebox,where I am going wrong</p> <pre><code> public partial class RemoveAds : Form { OpenFileDialog ofd = null; string path = @"C:\Users\Monika\Documents\Visual Studio 2010\Projects\OnlineExam\OnlineExam\Image\"; // this is the path that you are checking. public RemoveAds() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (System.IO.Directory.Exists(path)) { ofd = new OpenFileDialog(); ofd.InitialDirectory = path; DialogResult dr = new DialogResult(); if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Image img = new Bitmap(ofd.FileName); string imgName = ofd.SafeFileName; txtImageName.Text = imgName; pictureBox1.Image = img.GetThumbnailImage(350, 350, null, new IntPtr()); ofd.RestoreDirectory = true; } } else { return; } } private void button2_Click(object sender, EventArgs e) { //Image img = new Bitmap(ofd.FileName); string imgName = ofd.SafeFileName; if (Directory.Exists(path)) { var directory = new DirectoryInfo(path); foreach (FileInfo file in directory.GetFiles()) { if(!IsFileLocked(file)) file.Delete(); } } } public static Boolean IsFileLocked(FileInfo path) { FileStream stream = null; try { //Don't change FileAccess to ReadWrite, //because if a file is in readOnly, it fails. stream = path.Open ( FileMode.Open, FileAccess.Read, FileShare.None ); } catch (IOException) { //the file is unavailable because it is: //still being written to or being processed by another thread //or does not exist (has already been processed) return true; } finally { if (stream != null) stream.Close(); } //file is not locked return false; } } </code></pre> <p>Thanks in advance for any help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload