473,388 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to fix "file is being used by another process" errors?

I've got a problem wich cant manage to overcome although it might seem fairly simple. I've got a chat program, in wich i want to enable users to change user pics at runtime. There's a picture box winch is set to an image with the name of a certain peer for example : "C:\\FriendLy\\Users\\eran\\Pictures\\eva.jpg"

Now what i need is to replace this picture and override it. The first thing i do is copy it to a new location (becuase i dont want to loose it)

1) i find if theres a picture for the user "eva" in the designated path .

Expand|Select|Wrap|Line Numbers
  1. String[] files = Directory.GetFiles(user_pic_path);
  2.         if (files.Count<String>() > 0)
  3.         {
  4.           FileInfo _info = new FileInfo(files[0]);
  5.           bool found = false;
  6.           foreach (String _file in files)
  7.           {
  8.             _info = new FileInfo(_file);
  9.             if (_info.Name.Equals(_peer_name + _info.Extension))
  10.             {
  11.               found = true;
  12.               _fpath = _info.FullName;
  13.               break;
  14.             }
  15.           }
2) if found i copy it to a diffrent locaition , i dont use File.Move for the same reason i post this problem (the same System.IO.Exception) claming that it is in use
altho i made sure to remove it from any other user before attempting to move it .


Expand|Select|Wrap|Line Numbers
  1. PeerContents updated_contents = getPeerContents(_peer_name); ;        
  2.         updated_contents.img = null;
  3.         picbox_peer.Image = null;
  4.  
  5.  3) as i was saying if it was found it gets copied to a new location 
  6.  
  7. if (found)
  8.           {
  9.             String files_path = Path.Combine(received_files_path, _info.Name);
  10.             overrideOk = false;
  11.             int i = 0;
  12.             String ext = _info.Extension;
  13.             String nm = _info.Name; 
  14.             _info = null;
  15.  
  16.             while (!overrideOk)
  17.             {
  18.               try
  19.               {
  20.                 File.Copy(_fpath,files_path,false);
  21.                 overrideOk = true;
  22.               }
  23.               catch (Exception es)
  24.               {
  25.                 i++; 
  26.                 String f_name = nm.Substring(0,nm.Length - ext.Length);
  27.                 files_path = Path.Combine(received_files_path, f_name + "(" + i + ")" + ext);
  28.               }
  29.             }                                    
  30.           }
4) now i try to copy the new file and override the old "eva.jpg" file , i choose a file using OpenFileDiaglog and copy it to the designated user pictures path destination
with override set to true , at this point i get the same System.IO.Exception claming that it is in use some by anouther process ( IT AINT! ..as far as i can tell )

..it wont let me delete it for the same reason...

Expand|Select|Wrap|Line Numbers
  1. String fileName = openFileDialog1.FileName;
  2.         FileInfo new_info = new FileInfo(fileName); 
  3.         new_path = Path.Combine(user_pic_path,_peer_name + new_info.Extension);
  4.         overrideOk = false; 
  5.         while (!overrideOk)
  6.         {
  7.            try
  8.            {
  9.              File.Copy(fileName, new_path, true);
  10.              overrideOk = true;
  11.            }
  12.            catch (Exception es)              
  13.            {
  14.              File.Delete(new_path);
  15.            }
  16.         }
The process cannot access the file 'C:\FriendLy\Users\eran\Pictures\eva.jpg' because it is being used by another process.
if any one can think of the reason or knows how i can check what process holds the file,it would be most helpfull
Jan 29 '11 #1
1 26170
It looks like someone else had this question as well. Maybe the replies they got will help...

http://bytes.com/topic/c-sharp/answe...-deleting-file

But to summarize, I think your best bet would be to either try disposing the PictureBox's Image before doing file operations, or reading the file into an Image object in memory and using that for the Image property instead of the file itself.
Jan 30 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Carl Owenby | last post by:
Re: Microsoft Knowledge Base Article - 174942 "File Modified Outside Source Editor" Warning Message I believe I have discovered a cause of this warning in a specific situation. I encountered...
1
by: Erialc Berts | last post by:
I know why we get this error, but does anyone know how to catch it so that we can display a more helpful message to our users? I am interested both in a .exe and a web site. Thanks in advance...
4
by: Earth Worm Jim | last post by:
I am using VS.Net 2003 on Windows 2003 Server (standard edition) and I am getting "The process cannot access the file because it is being used by another process" on DLL's in a VS.Net solution. ...
3
by: kris.dorey | last post by:
Hi, Ive got the following code which seems ok but when the user runs the function for a second time I get an error message stating that the mdb is in use by another process. There is still an...
3
by: Bob | last post by:
I've been repeatedly annoyed by situations where a process has failed or otherwise something has gone wrong (not from any of my own apps) where I cannot move, rename, or delete a file because...
3
by: Loane Sharp | last post by:
Hi there I use the FileStream object to download a zip file over the internet to my local disk. The file downloads successfully, but when I attempt to unzip it, I'm told that the file is in use...
2
by: Goran Djuranovic | last post by:
Hi all, I was getting this error when trying to move files on a FileSystemWatcher notification (with in a Windows Service). To fix this, I implemented a FileWaiter class to wait for the file to be...
7
by: Robinson | last post by:
Hi, I was just playing around with my log files and tried to open a log file programmatically that was considered "in use" (I got an in-use exception). The file is being used by my debug writer...
1
by: othgwayne | last post by:
I am writing in .NET VB 2003 I have an app that uses a file system watcher looking for a file to fall into this directory. When the file falls, it is opened, processed, closed and deleted. Then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.