Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working in ASP.NET application. when I try to change the profile picture (by using "Edit" function) I get the error: "The process cannot access the file 'C:\WebSite\sitename\ProviderImages\77ab2-e.png' because it is being used by another process.". I cannot Change the profile picture file . My code is following:





Thanks.............
C#
if (FileUpload1.HasFile)
                        {
                            System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
                            if (UploadedImage.Width < 100 || UploadedImage.Height < 150)
                            {
                                string filename = Guid.NewGuid().ToString().Substring(0, 10);
                                string extension = FileUpload1.PostedFile.FileName.Remove(0, FileUpload1.PostedFile.FileName.LastIndexOf("."));
                                FileUpload1.SaveAs(Server.MapPath(Request.ApplicationPath) + "/ProviderImages/" + (filename + extension));
                                string path = Server.MapPath(Request.ApplicationPath) + "/ProviderImages/" + (filename + extension);
                                string path1 = Server.MapPath(Request.ApplicationPath) + "/ProviderImages/" + ("tmp" + filename + extension);
                                
if (ThumbNailGenerator.GenerateThumbNail(path, path1) == true)

                                {


if (HiddenField1.Value != null)
                                   {
                                       if (File.Exists(Server.MapPath(Request.ApplicationPath + "/ProviderImages/" + HiddenField1.Value)))
                                       {
                                           File.Delete(Server.MapPath(Request.ApplicationPath + "/ProviderImages/" + HiddenField1.Value));

                                           if (File.Exists(Server.MapPath(Request.ApplicationPath + "/ProviderImages/" + "tmp" + HiddenField1.Value)))
                                           {
                                               File.Delete(Server.MapPath(Request.ApplicationPath + "/ProviderImages/" + "tmp" + HiddenField1.Value));
                                           }
                                       }

                                   }

                                   updateproviderrequest.Provider.Company.CompanyLogo = filename + extension;
                                   FileUpload1.FileContent.Dispose();

                                   updateproviderresponse = providerservice.Update(updateproviderrequest);
                               }
                           }
                           else
                           {
                               lblmessage.Text = "";
                               lblmessage.Text = "Upload image lessthan 100*150 size";
                               return;

                           }
                       }
                       else if (FileUpload1.HasFile == false)
                       {
                           updateproviderrequest.Provider.Company.CompanyLogo = string.Empty;
                           updateproviderresponse = providerservice.Update(updateproviderrequest);
                       }
                       else
                       {
                           getEmployeeRequest.UserId = (Guid)Session["UserId"];
                           getEmployeeRequest.CompanyName = txtcompanyname.Text;
                           getEmployeeResponse = EmplyeeService.Get(getEmployeeRequest);
                           if (getEmployeeResponse.AlreadyEmailErrorMessages.Count > 0)
                           {
                               lblcommsg.Text = "CompanyName Exist..Please Enter again";
                               lblmessage.Text = string.Empty;
                               return;
}
Posted
Updated 29-Oct-12 23:09pm
v3

1 solution

Hi,

The error is coming due to the object is not disposed.
If you are copying file then after complete the copy you have to dispose that object.
Try using the disposing the objects.
May be it will solve the problem.

If not then share your code here.

try here dispose UploadedImage object.
if you use using then you don`t need to exposed the objects.
 
Share this answer
 
v2
Comments
Jagadeesh Ballamudi 30-Oct-12 7:47am    
how to dispose UploadedImage object
VIPR@T 30-Oct-12 7:57am    
UploadedImage.Dispose();

also use 'using' like this.

using(File.Delete(Server.MapPath(Request.ApplicationPath + "/ProviderImages/" + HiddenField1.Value)));
Jagadeesh Ballamudi 30-Oct-12 8:14am    
its not working.where UploadedImage.Dispose();useing in this code
VIPR@T 30-Oct-12 8:23am    
have you use 'using' which i mentioned above?
Jagadeesh Ballamudi 30-Oct-12 8:25am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900