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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eran otzar
    New Member
    • Jan 2011
    • 13

    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\\Pi ctures\\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 .

    Code:
    String[] files = Directory.GetFiles(user_pic_path);
            if (files.Count<String>() > 0)
            {
              FileInfo _info = new FileInfo(files[0]);
              bool found = false;
              foreach (String _file in files)
              {
                _info = new FileInfo(_file);
                if (_info.Name.Equals(_peer_name + _info.Extension))
                {
                  found = true;
                  _fpath = _info.FullName;
                  break;
                }
              }
    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.Excep tion) claming that it is in use
    altho i made sure to remove it from any other user before attempting to move it .


    Code:
    PeerContents updated_contents = getPeerContents(_peer_name); ;        
            updated_contents.img = null;
            picbox_peer.Image = null;
    
     3) as i was saying if it was found it gets copied to a new location 
    
    if (found)
              {
                String files_path = Path.Combine(received_files_path, _info.Name);
                overrideOk = false;
                int i = 0;
                String ext = _info.Extension;
                String nm = _info.Name; 
                _info = null;
                
                while (!overrideOk)
                {
                  try
                  {
                    File.Copy(_fpath,files_path,false);
                    overrideOk = true;
                  }
                  catch (Exception es)
                  {
                    i++; 
                    String f_name = nm.Substring(0,nm.Length - ext.Length);
                    files_path = Path.Combine(received_files_path, f_name + "(" + i + ")" + ext);
                  }
                }                                    
              }
    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.Excep tion 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...

    Code:
    String fileName = openFileDialog1.FileName;
            FileInfo new_info = new FileInfo(fileName); 
            new_path = Path.Combine(user_pic_path,_peer_name + new_info.Extension);
            overrideOk = false; 
            while (!overrideOk)
            {
               try
               {
                 File.Copy(fileName, new_path, true);
                 overrideOk = true;
               }
               catch (Exception es)              
               {
                 File.Delete(new_path);
               }
            }
    The process cannot access the file 'C:\FriendLy\Us ers\eran\Pictur es\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
    Last edited by Niheel; Jan 30 '11, 12:30 AM.
  • bentleyw
    New Member
    • Feb 2008
    • 32

    #2
    It looks like someone else had this question as well. Maybe the replies they got will help...



    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.

    Comment

    Working...