简体   繁体   中英

UnauthorizedAccessException only when overwriting a file

We are in process of migrating an application from ASPX to MVC and recently deployed first phase. So we have a mix of ASPX and MVC pages currently.

We have some features that read/write data to files. The code is using FileStream to open, write content and save to disk. The code overwrites the previously created files. This code has been working for ever. However, soon after MVC deployment, the same code won't overwrite the files that were created a day before but will overwrite if the file was created on same day.

So if I manually delete a file that was created yesterday, it will start to work fine and will overwrite the file if I run the same feature over and over again. but if I run the feature tomorrow, it won't overwrite and throw System.UnauthorizedAccessException .

Here is the stack trace:

System.UnauthorizedAccessException: Access to the path 'D:\wwwroot\abc.xml' is denied.  
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)  
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)  
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)  
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)  
   at System.Xml.XmlDocument.Save(String filename) 

Please help...

Please check Identity of AppPool that is in use. That userid should have full control access to the directory where you are doing this reading and writing operation.... This could be one reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM