Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends,


i am trying to update student profile picture once i update the photo i am getting one Error which i have showed below. please tell me guys if anyone knows


Error:"
The process cannot access the file '1.png' because it is being used by another process.
"


for this Error Instead of
var directory = new DirectoryInfo(path);
              directory.Delete(true);
this line i have used
Directory.Delete(path);
but agin i am getting Error like

Error:
The directory is not empty.


What I have tried:

public static string ImageToServerPath(student[] student)
       {
           DataImage image = DataImage.TryParse(student[0].path);
           MemoryStream ms = new MemoryStream(image.RawData);
           Bitmap BMP = new Bitmap(image.Image);

           string configPath = WebConfigurationManager.AppSettings["Path"];

           string path =configPath+""+student[0].studentId+"\\";

           if (!Directory.Exists(path))
           {
               Directory.CreateDirectory(path);
           }
           else
           {
               Directory.Delete(path);
               Directory.CreateDirectory(path);
           }

           FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, path);

           if (SecurityManager.IsGranted(writePermission))
           {
               int imagename = 1;
               System.Drawing.Image img = new System.Drawing.Bitmap(ms);
               img.Save(path + student[0].studentId + ".png", ImageFormat.Png);
               path = path + "" + imagename + ".png";
           }
           return path;
       }
Posted
Updated 21-Jun-17 6:49am

1 solution

I think you need to close the MemoryStream after using it:
C#
using (MemoryStream ms = new MemoryStream(image.RawData);) 
{  
    BMP = new Bitmap(image.Image);
}
I would also take a look at:
C#
DataImage image = DataImage.TryParse(student[0].path);
As I don't have your source code, I can not tell what is happening there ...
 
Share this answer
 
v2
Comments
Richard Deeming 22-Jun-17 16:34pm    
I suspect DataImage is the class I posted three weeks ago:
Check base64 string(image) type using C#[^]

Unless it's been changed, it shouldn't have anything to do with locking files.

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