This is working fine in Visual Studio but not in Localhost won't work. So first check the reason behind that, then you will also able to understand for your domain as well.
When your application is running from Visual Studio, your application having sufficient
privilege
to write of your hard drive, hence your application is running fine as it is able to create file.
Now when you are hosting the files in IIS, Application pool comes into the picture.
Application pool having 3 types of identity.
1. NetworkService<br />
2. Local Service<br />
3. Local System
"
Network Service
" is the default Identify. "
defaultappPool
" is also runs under the "Network Service" Identity
By default your application pool runs on "
NetworkService
" account which has very minimum access to your drive, only read. So, When your application trying to write a file on drive, its getting access denied.
The solution, is give the Application Pool with "
LocalSystem
" privilege or Run your application pool with the account who have write access on drive.
So, create a new Application Pool for your application, Change the Identity to "
LocalSystem
" or Run on High Privilege account.
To know more about "Application Pool and the Identity" Read this section from one of my article
Beginner's Guide : Exploring IIS 6.0 With ASP.NET [^][
^]
In your hosting env, check the privilege to account on which your Application is running on.