cant access a file using C# asp mvc application

I have an mvc asp application that needs to edit a file by the name of test.php I didnt have any error using this method in my LocalHost , when i transfered it to server i get this error :

System.UnauthorizedAccessException: Access to the path 'C:\Users\Administrator\Desktop\codeEditor\test.php' 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, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost) at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost) at System.IO.File.ReadAllText(String path) at ASP._Page_Areas_user_Views_php_dark_cshtml.Execute() in C:\Inetpub\vhosts\tutpersia.com\httpdocs\Areas\user\Views\php\dark.cshtml

I tried changing permissions of my directory using Plesk or by right click and ... but still the same error

what Im trying to do in my app is :

  string filePath = "C:\\Inetpub\\vhosts\\sth.com\\httpdocs\\test.php";

            string text = System.IO.File.ReadAllText(filePath);
            System.IO.File.WriteAllText(filePath, text+"sth");

thank you all

1 answer

  • answered 2018-04-17 05:49 dsdel

    You need to set the authorization to allow the IIS_IUSRS. Please also force the permission to be applied on all subfolders/files.

    Be careful what you allow to IIS_IUSRS due to security.