0

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?
  • The account the appliction pool is running under needs to have permissions to read/write that file - that's the first place I'd look. – Tim Aug 07 '13 at 10:15
  • Check if you have permissions to the temporary internet file location – lloydom Aug 07 '13 at 10:15
  • This code has problem on `localhost` too. This is not just problem from my server. –  Aug 07 '13 at 10:27
  • This ist not a permission issue by the looks of it. The file you want to use is in use by the time you want to access it. Find the process which is using the file and kill it. This thread might help you: http://stackoverflow.com/questions/317071/how-do-i-find-out-which-process-is-locking-a-file-using-net – Marco Aug 08 '13 at 07:43

2 Answers2

1

1)Your code is correct

2)Probably this is because the file you are trying to replace is set to "read-only" , change it and try to replace it

3)Don't know

4)You can try any custom upload method or using ajax,it will be faster but not simple as asp.net file upload

5)I think this is a good one

Ahmed Alaa El-Din
  • 1,813
  • 1
  • 16
  • 19
  • This website uses on many servers for many customers that I cant change configuration and settings of those servers. Do you know any way to change settings with my website and use it on many server with different settings? –  Aug 07 '13 at 10:20
  • Is there any way to change setting of IIS Express? –  Aug 07 '13 at 10:32
  • 1
    My own answer completes your answer. –  Sep 18 '13 at 14:39
  • Hi... Thanks for your attention. Please delete your answer because I wanna delete my question. Thank you! –  Jul 06 '14 at 10:43
0

Try this :- right click on folder in which file placed and give full access

  • Any solution to change access options from my website? Folder is **Full Permission** for **Everyone**. Your answer was wrong! –  Aug 08 '13 at 04:02
  • tried. Excuse me but not worked. _Its great way that change access and permissions from server (Windows) but I need a way from my website too._ **Cant access the file because file is in use.** –  Aug 08 '13 at 04:37
  • Change the port number in IIS binding may be another application using same port number – Subhash Jakhar Aug 08 '13 at 04:49
  • Cant help. Problem isn't here. It works nice on Development Server. Problem isn't just from my Windows IIS. When I test it on WebMatrix (IIS Express 8) has this problem too. –  Aug 08 '13 at 04:51
  • You file is used and open in another process so please check in task manager IIS restart recycle and visits this url may be help you :-http://www.codeproject.com/Questions/295198/The-process-cannot-access-the-file-because-it-is-b – Subhash Jakhar Aug 08 '13 at 04:56
  • The question on [CodeProject](http://www.Codeproject.com) was same as my question. But cant help me because this website should work on server and my costumers don't know anything about IIS or managing tags. It should be noted that I know many huge upload centers that overwrite files without any problem. My uploads are simple images! –  Aug 08 '13 at 05:10
  • try this http://dotnetguts.blogspot.in/2009/07/process-cannot-access-file-because-it.html – Subhash Jakhar Aug 08 '13 at 05:47
  • Nice. But not worked. Question updated. Please read updated question and help me... –  Aug 08 '13 at 06:49
  • Dear you need to dispose or close object http://www.brianstevenson.com/blog/solution-the-process-cannot-access-the-file-filename-because-it-is-being-used-by-another-process – Subhash Jakhar Aug 08 '13 at 08:16