hello ,
ive got a problem wich cant manage to overcome altho it might seem farily
simple ,
ive got a chat program, in wich i want to enable users to change user pics at
runtime
there's a picturebox wich is set to an image with the name a
certine 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 locaition (becuase i dont want to loose
it)
1) i find if theres a picture for the user "eva" in the designated
path .
String[] files =
Directory.GetFiles(user_pic_path);
if
(files.Count
{
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.Exception) claming that it is in use
altho i made sure to remove it from any other user before
attempting to move it .
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.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...
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\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
Suthish NairPosted Jan 30, 2011, 1:11 PM
Sam HobbsPosted Jan 30, 2011, 1:02 PM
Suthish NairPosted Jan 30, 2011, 4:31 AM
If you want to make sure the file still in use, refer below link.
this will throw an exception if file already in use
If you dont need that process then follow this one.
Killing Processes from a Windows Form Application in C#
Sam HobbsPosted Jan 29, 2011, 8:49 PM
FroglegPosted Jan 29, 2011, 4:32 PM
Even though picbox_peer.Image is null , eva.jpg is still being used by program.
Try loading image img1, cloning it, img2 and then disposing img1.