I have an ASP.NET web form website. This code runs when I press upload button. This code have many problems. Its not fast and simple and it have a big problem. For first time image upload, it works nice but when I want to replace a file returns Access denied
error. I have this error on IIS express but it works correctly on Visual Studio Development Server.
note: I have grid view that shows which user image must be changed; This grid view also help me to find the specific user image file.
if (UsersImageFileUpload.HasFile && UsersImageFileUpload.PostedFile.ContentType.StartsWith("image"))
{
string filename = UploadUsersImageGridView.SelectedRow.Cells[1].Text.ToString();
if (!File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
{
UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Users/") + filename);
}
else if (File.Exists(Server.MapPath("~/App_Data/Backups/" + filename)))
{
File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
UsersImageFileUpload.PostedFile.SaveAs(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
File.Replace(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"), Server.MapPath("~/App_Data/Users/") + filename, Server.MapPath("~/App_Data/Backups/"));
File.Delete(Server.MapPath("~/App_Data/Backups/" + filename + " (Replace)"));
}
}
}
Error Title:Server Error in '/' Application
.
Error Second Title:The process cannot access the file 'D:\Website\App_Data\Users\1-fullname' because it is being used by another process.
Error Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Error Details:System.IO.IOException: The process cannot access the file 'D:\App_Data\Users\1-fullname' because it is being used by another process
My Questions:
- Whats problem with my code?
- Why IIS returns that error (access to file is denied)?
- Whats the most important difference between IIS Express 8 And Visual Studio 2012 Development Server?
- Any faster way to create upload and replace?
- Any better form to have this system?