PDA

View Full Version : مشکل در حذف یک فایل



ramin149
سه شنبه 06 مهر 1389, 13:46 عصر
من یک فایل رو در یک فولدر آپلود می کنم و بعد اون فایل رو میگیرم و تغییر سایز می دهم و در جای دیگه کپی می کنم ولی وقتی می خوام فایل قبلی رو پاک کنم با این کد


String strPath = HttpContext.Current.Server.MapPath(@"~//photos/Oldphoto/" + filename);
System.IO.File.Delete(strPath);

چنین خطایی بهم می ده


The process cannot access the file 'H:\Web Site\panel\photos\Oldphoto\Koala.jpg' because it is being used by another process.

به نظر شما من باید چه کار کنم ؟؟؟

karim orooji
سه شنبه 06 مهر 1389, 14:08 عصر
با سلام اگه برنامه روبزاری بهتره
من با این خطا در برنامه مواجه شدم
میگه عکس شما هنور داره روش کار انجام میشه
باید با متد Dispose و یا nothing ببندیش
بعد عملیات دیگری رو روش انجام بدی

ramin149
سه شنبه 06 مهر 1389, 14:17 عصر
این کد کل کلاسم



public class photos
{
private Label Error = new Label();
private FileUpload fileup = new FileUpload();
public photos(FileUpload file , Label lbl)
{
this.fileup=file;
this.Error=lbl;
}
public void Upload(Label Error)
{
string mypath = HttpContext.Current.Server.MapPath(".") + "//photos//OldPhoto//";
string mybanner = HttpContext.Current.Server.MapPath(".") + "//photos//photo";
string [] validextention={".jpg" , ".png" , ".bnp",".jpeg"};
string x = System.IO.Path.GetExtension(fileup.PostedFile.File Name);
if ( Array.IndexOf(validextention,x.ToLower()) <0 )
{
Error.Text="you seleted invalid type of image for uploading";
return;
}


long sizefile = fileup.PostedFile.ContentLength;
sizefile = sizefile / 1024;
if (sizefile > 200)
{
Error.Text="invalid size image uploading";
return;
}

string filename = System.IO.Path.GetFileName(fileup.PostedFile.FileN ame);
while(System.IO.File.Exists(mypath+filename))
{
int c = filename.Length - x.Length;
filename = filename.Insert(c, "3");

}

fileup.PostedFile.SaveAs(mypath + filename);
Error.Text="youfileuploaded";

new Bitmap(System.Drawing.Image.FromFile(HttpContext.C urrent.Server.MapPath(@"~//photos/Oldphoto/" + filename)), new Size(132, 82)).Save(HttpContext.Current.Server.MapPath(@"~/photos/admin/" + filename));


String strPath = HttpContext.Current.Server.MapPath(@"~//photos/Oldphoto/" + filename);
System.IO.File.Delete(strPath);
}
}

ramin149
سه شنبه 06 مهر 1389, 15:10 عصر
کسی نیست به ما کمک کنه ؟؟؟؟

ramin149
سه شنبه 06 مهر 1389, 17:17 عصر
من کد بالا رو به این صورت تغییر دادم و dispost رو هم اضافه کردم ولی هنوز همون مشکل رو داره ؟؟؟


Bitmap sphotos = new Bitmap(System.Drawing.Image.FromFile(HttpContext.C urrent.Server.MapPath(@"~//photos/Oldphoto/" + filename)));
ResizePicture(sphotos, new Size(132, 82)).Save(HttpContext.Current.Server.MapPath(@"~/photos/admin/" + filename));
sphotos.Dispose();
String strPath = HttpContext.Current.Server.MapPath(@"~//photos/Oldphoto/" + filename);
System.IO.File.Delete(strPath);
public Bitmap ResizePicture(Bitmap sourceImage, Size newSize)
{
Bitmap Result_image = new Bitmap(sourceImage, newSize.Width, newSize.Height);
Graphics Gr = default(Graphics);
Gr = Graphics.FromImage(Result_image);
Gr.DrawImage(Result_image, 0, 0, newSize.Width, newSize.Height);
Gr.Save();
return Result_image;
}

ramin149
چهارشنبه 07 مهر 1389, 10:18 صبح
تو رو خدا یکی کمک کنه من فقط لنگ همینم