I have a fairly simple set of controls that make up an image viewer.

The goal of this software is to allow image captures to be stored in a temporary directory, viewed in the image viewer, and then have the user able to choose to delete or save the contents of the temporary directory.

Deleting is easy.

But whenever I try to save the information (by moving the temporary directory to the user-defined saved directory), I get the System.IOException error that the operation can't be done because the temporary folder is still in use.

This is driving me nuts.

I have a picture box (pbCaptureImage) with an image.Fromfile (the image I wish to show), a label (lPlaybackName) with the file name (not the entire path) of the image, a text box (lblPBFrame) that has the current image + (As in, "2 of 40"), and a track bar (tbplayback) that has a tick for every image in the directory.


After a capture session, the application saves all the pictures taken to the harddrive in a folder called "uBtemp", and allows the user to look through the images. This is performed in a function called "WriteTemp().

However, if I try to save the data by using the 'save' button to try renaming the existing directory (by using Directory.Move), the program tells me it cannot access the temporary directory.

If I edit out the setup for the trackbar and image capture, there is no problem, but I dont' get the intended functionality of allowing the user to look at the images beforehand.

Oh, and PastNames is an arraylist of the file paths without the extensions.


Here is the code, if anyone can suggest anything.

/// <summary>
		/// This function:
		/// 1. Saves all the data from a continuous capture into a temporary directory called uBTemp.
		/// 2. Loads the information into the playback.
		/// 3. Posts a messagebox asking user to save or delete the data
		/// 4. Makes pSaveDelete.Visible=true;
		/// 
		/// NOTE: Make sure to lock down/hide/deactivate Acquire/Next Scan buttons during this process.	
		///
		/// local_config.FileName has the file name from the settings, IE, Datas02_1
		///	local_config.path1 has the parent directory for saves,     IE, C://desktop//documents and settings// etc.	 
		/// local_config.path2 has the current save directory.		   IE, as path 1, but with Datas02 directory at end.
		/// </summary>
		private void WriteTemp()
		{
			this.bNextScan.Enabled=false;
			this.bAcquire.Enabled=false;

			axDTAOLFG1.ActiveFrame=0; 
			int ptr ;
			this.Settings.IncrimentPath2(0);				            //Is this necessary or correct in this context?
			this.Settings.IncrimentFileName1();
			
			if(Directory.Exists(local_config.path1+"\\uBTemp\\"))
				Directory.Delete(local_config.path1+"\\uBTemp\\",true);
			Directory.CreateDirectory(local_config.path1+"\\uBTemp\\");

			for(int i = 0; i<TotalFramesAcquired;i++)
			{				
				pbarCapture.Value=i;
				FilePathUpdate();

				fName="";
				fName=local_config.path1+"\\uBTemp\\"+lFileName.Text;
				//fName=lFilePath.Text + "/" + lFileName.Text;
				fName=fName.Replace("\\","/");
					
				axDTAOLFG1.ActiveFrame=i;					
				ptr=axDTAOLFG1.FrameBaseAddress;

				if(0==lead.SaveJPG(fName+".jpg",ptr,axDTAOLFG1.FrameWidth,axDTAOLFG1.FrameHeight))
					MessageBox.Show("There is a problem saving frame " +i.ToString());
				else
				{
					this.PastNames.Add(fName);
						
					if(this.mIBirdOn.Checked)
						WriteData(PastNames[i]+".brd",(PositionCollectionBase)BirdPos[i],(DateTime)Times[i]);
				}
					
				this.Settings.IncrimentFileName1();					
			}			
		
//Commenting out the following  if/else statement lets the directory be moved, at the expense of being able to view files. 
			//tbPlayback.Maximum=this.PastNames.Count-1;
			if(tbPlayback.Maximum==0)
			{
				fName=(string)PastNames[tbPlayback.Value];
				//fName=fName.Substring(this.lFilePath.Text.Length+1);
				fName=fName.Substring(this.local_config.path1.Length+1);
				this.lPlaybackName.Text	= fName+".jpg";
				this.lblPBFrame.Text=(tbPlayback.Value+1).ToString()+"	of "+ (PastNames.Count).ToString();
				pbCaptureImage.Image=(Bitmap)Image.FromFile(this.PastNames[tbPlayback.Value]+".jpg");								
			}
			else
				tbPlayback.Value=PastNames.Count-1;	//updates playback bar and information.	
				
				

			bStop.SendToBack();
			stop = false;


            this.pSaveDelete.Visible=true; //Enable user to pick Save or Delete.
			NextScan(0);				
		}
/// <summary>
        /// 1. Clears the playback bar of existing information.
        /// 2. Check for the existence of a directory with the current pathname
        ///		If one exists, throw message box up to determine course of action
        /// 3. Renames the Temporary folder to the correct pathname.   
        /// 4. Reloads information for playback.    
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		private void bSave_Click(object sender, System.EventArgs e)
		{
			if(this.pSaveDelete.Visible==true) //Ensure this behavior will not be triggered at the wrong time.
			{
				this.pSaveDelete.Visible=false; //Hide panel to prevent meddling during process.
				
				fName="";//fName.Substring(this.local_config.path1.Length+1);
				this.lPlaybackName.Text="";// fName+".jpg";
				this.lblPBFrame.Text="";//(tbPlayback.Value+1).ToString()+"	of "+ (PastNames.Count).ToString();
				//pbCaptureImage.Image=(Bitmap)Image.FromFile(this.PastNames[tbPlayback.Value]+".jpg");								
				pbCaptureImage.Image=(Bitmap)Image.FromFile(Directory.GetCurrentDirectory()+"\\loadsplash.jpg");
				//pbCaptureImage.
				//this.tbPlayback.Value = 0;
				//this.tbPlayback.Maximum= 0;
			

				
				
				string tempstring=local_config.path1+"/uBTemp/";
				int tempint = tempstring.Length;
				//NOTE: NEED TO MAKE SURE PASTNAMES HAS NEW FILE PATH! 
				for(int i = 0; i<PastNames.Count;i++)
				{
					tempstring = (string)PastNames[i];  //Might be able to get rid of 'temp' by casting Pastnames as string and doing replace directly.
					tempstring=tempstring.Remove(0,tempint-1);
					//temp.Replace(local_config.path1+"\\uBTemp\\",local_config.path2); //THIS COULD FAIL.
					PastNames[i]=local_config.path2+tempstring;
				}
				

			    if(Directory.Exists(this.lFilePath.Text))
					Directory.Delete(this.lFilePath.Text,true);
				
				
				
				Directory.Move(local_config.path1+"\\uBTemp",this.lFilePath.Text); //Program throws exception here.

				

				this.Times.Clear();  //Times have already been saved as a file, no need to worry about this.
					//this.ClIArray.Clear();

			
				this.ticks.Clear();
				
				tbPlayback.Maximum=this.PastNames.Count-1;
				if(tbPlayback.Maximum==0)
				{
					fName=(string)PastNames[tbPlayback.Value];
					
					fName=fName.Substring(this.local_config.path1.Length+1);
					this.lPlaybackName.Text	= fName+".jpg";
					this.lblPBFrame.Text=(tbPlayback.Value+1).ToString()+"	of "+ (PastNames.Count).ToString();
					pbCaptureImage.Image=(Bitmap)Image.FromFile(this.PastNames[tbPlayback.Value]+".jpg");								
				}
				else
					tbPlayback.Value=PastNames.Count-1;	//updates playback bar and information.	
					


				
				this.bAcquire.Enabled=true;
				this.bNextScan.Enabled=true;
				NextScan(0);
			}		
		}

After further research, it appears that the problem is likely due to file share settings. That is to say, it is currently only allowing one process to access the file.

This gives me a few options on what to explore as solutions.
1. Find out how to release access (Preferred)
2. Open up the ability to share the active file, change the image being used to a splash screen, and then move the file... Probably not a great idea.
3. As 2, only with copying everything to a new directory, rather than renaming... (Last resort)

To follow up on 1, anyone know if/how you can use a FileInfo reference to load an image into a picturebox control, in a way that allows the fileshare permissions to be changed?

Thanks in advance.

Modified 'bSave_Click' to include pbCaptureImage.Image.Dispose() before attempting to rename the directory.

This forces the image to be removed from 'active' or 'accessed' status and lets the garbage collector take care of things prior to the renaming.

This question has already been answered. Start a new discussion instead.