The process cannot access the file D:MainPicturesimage.jpg because it is being used by another p

Last Reply on Apr 23, 2014 07:02 AM By Azim

Posted on Apr 23, 2014 07:00 AM

aspx.cs

protected void btnsubmit_Click(object sender, EventArgs e)

{
string imgname = Path.GetFileName(imageupload.PostedFile.FileName);
string folderPath = Server.MapPath(string.Format("MainPictures"));

if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
imageupload.SaveAs(Server.MapPath("MainPictures/" + imgname));
imagepath = Server.MapPath("MainPictures/" + imgname);

WPFExifInfo.ExifMetaInfo classfile = new WPFExifInfo.ExifMetaInfo(new Uri(imagepath));
string aper = classfile.LensAperture.ToString();
string isospeed = classfile.IsoSpeed.ToString();
decimal shutter = Convert.ToDecimal(classfile.ExposureTime);
string focal = classfile.FocalLength.ToString();

if (aper == "")
{
aperture = "-";
}
else
{
aperture = "F/" + aper;
}
if (isospeed == "")
{
iso = "-";
}
else
{
iso = isospeed;
}
if (shutter == 0)
{
totalshutter = "-";
}
else
{
shutterspeed = shutter;
int value1 = 1;
decimal mediator = value1 / Convert.ToDecimal(shutterspeed);
totalshutter = "1/" + Convert.ToString(mediator);

}
if (focal == "")
{
focallength = "-";
}
else
{
focallength = focal + "mm";
}

SqlConnection con = new SqlConnection(CS);
con.Open();
try
{
System.Drawing.Image uploadImage = System.Drawing.Image.FromStream(imageupload.PostedFile.InputStream);
System.Drawing.Image imgFile = System.Drawing.Image.FromStream(imageupload.PostedFile.InputStream);
int fileLenght = imageupload.PostedFile.ContentLength;
string newname = DateTime.Now.ToString("MMddyyyyhmmss");
string autoid = lblrandomid.Text;
int userid = Convert.ToInt32(Session["UserId"]);
string Pname = Convert.ToString(Session["Pname"]);
string ApproveStatus = "0";
SqlCommand cmd = new SqlCommand("insert into photography(PictureURL,Pname,Category,CamModel,Lens,Aperture,ISO,ShutterSpeed,FocalLength,AuthorComments,Keywords,Pdate,UserId,Email,ApproveStatus,Title,ImageType,likecounts) values(@PictureURL,@Pname,@Category,@CamModel,@Lens,@Aperture,@ISO,@ShutterSpeed,@FocalLength,@AuthorComments,@Keywords,@Pdate,@UserId,@Email,@ApproveStatus,@Title,@ImageType,@likecounts)", con);


HttpFileCollection fc = Request.Files;
for (int i = 0; i < fc.Count; i++)
{
HttpPostedFile hpfile = fc[i];
if (hpfile.ContentLength > 0)
{
string sDestinationPath = Server.MapPath("Pictures/");
string NewImageName = "P" + autoid.ToString() + "_" + newname.ToString() + System.IO.Path.GetExtension(hpfile.FileName);


if (radio1.Checked == true && imgFile.Width > 900)
{
ImageResizing.Image.Resize.create_1_image(hpfile, NewImageName, sDestinationPath, "900", "0");
cmd.Parameters.AddWithValue("@PictureURL", NewImageName);


}
else if (radio2.Checked == true && imgFile.Height > 900)
{
ImageResizing.Image.Resize.create_1_image(hpfile, NewImageName, sDestinationPath, "0", "900");
cmd.Parameters.AddWithValue("@PictureURL", NewImageName);


}
else
{
string filename = Path.GetFileName(imageupload.PostedFile.FileName);
string NewImageName1 = "P" + autoid.ToString() + "_" + newname.ToString() + System.IO.Path.GetExtension(filename);
imageupload.SaveAs(Server.MapPath("Pictures/" + NewImageName));
cmd.Parameters.AddWithValue("@PictureURL", NewImageName1);
}

cmd.Parameters.Add("@Title", SqlDbType.VarChar).Value = txtpname.Text;
cmd.Parameters.Add("@Category", SqlDbType.VarChar).Value = ddlcategories.SelectedItem.Text.ToString();
cmd.Parameters.Add("@CamModel", SqlDbType.VarChar).Value = txtcammodel.Text;
cmd.Parameters.Add("@Lens", SqlDbType.VarChar).Value = txtlens.Text;
cmd.Parameters.Add("@Aperture", SqlDbType.VarChar).Value = aperture;
cmd.Parameters.Add("@ISO", SqlDbType.VarChar).Value = iso;
cmd.Parameters.Add("@ShutterSpeed", SqlDbType.VarChar).Value = totalshutter;
cmd.Parameters.Add("@FocalLength", SqlDbType.VarChar).Value = focallength;
cmd.Parameters.Add("@AuthorComments", SqlDbType.VarChar).Value = txtauthorcomments.Text;
cmd.Parameters.Add("@Keywords", SqlDbType.VarChar).Value = txtkeywords.Text;
cmd.Parameters.Add("@Pdate", SqlDbType.VarChar).Value = txtdate.Text;
cmd.Parameters.Add("@UserId", SqlDbType.Int).Value = userid;
cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Session["Email"].ToString();
cmd.Parameters.Add("@ApproveStatus", SqlDbType.VarChar).Value = ApproveStatus;
cmd.Parameters.Add("@Pname", SqlDbType.VarChar).Value = Pname;
cmd.Parameters.Add("@likecounts", SqlDbType.Int).Value = 0;
if (radio1.Checked == true)
{
cmd.Parameters.Add("@ImageType", SqlDbType.VarChar).Value = radio1.Text;
}
else
{
cmd.Parameters.Add("@ImageType", SqlDbType.VarChar).Value = radio2.Text;
}
cmd.ExecuteNonQuery();
BindDetails();
clearfields();
lblmsg.ForeColor = System.Drawing.Color.Green;
lblmsg.Text = "Submitted Successfully!!!!";
}
}
}

catch (Exception ex)
{

}
finally
{
con.Close();
con.Dispose();
imageupload.FileContent.Dispose();
string delfolderpath = Server.MapPath(string.Format("MainPictures"));
if (Directory.Exists(delfolderpath))
{
if (System.IO.Directory.GetFiles(delfolderpath).Length > 0)
{
Array.ForEach(Directory.GetFiles(delfolderpath), File.Delete);
Directory.Delete(delfolderpath);
}
else
{
Directory.Delete(delfolderpath);
}
}
}
}
}


class.cs

http://www.codeproject.com/Articles/19438/Retrieving-EXIF-information-of-an-image-in-NET


I am getting error while deleting the folder as,

The process cannot access the file 'D:\MainPictures\image.jpg' because it is being used by another process.


please help me to solve this issue.

THANKS IN ADVANCE

 

Posted on Apr 23, 2014 07:02 AM

You need to write the code in Using block

Refer this link which is having the same issue.

http://stackoverflow.com/questions/12226179/web-application-not-able-to-delete-files-for-read-write-enabled-folders-in-asp-n

Thank You.

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html