Reputation: 825
I am writing an application in c# which saves data to xml. When i reopen the application it says
Access to the path 'C:\ProgramData\Test\abc.xml' is denied.
Can any one pls help me to sort out this problem.
Is there any way to find why access is denied.
Upvotes: 4
Views: 2476
Reputation: 25513
You're probably getting this error because you're running Vista or Win7 and you have UAC turned on.
C:\ProgramData requires admin priveledges to write to (but not read from).
I just found this out the hard way because I've been working on a program that was working fine on XP that used System.Environment.SpecialFolder.CommonApplicationData which in Vista refers to the above location and requires elevated privs to write to that directory.
Upvotes: 4
Reputation: 817
This is a common problem I've found with XML writer's in the past. If the Filestream being used isn't closed properly then the file will stay locked. I say this is a problem, mainly because I've noticed that for some reason the lock persists even after the application has been closed, but I've only seen it happen when I use an XML writer in this fashion (don't know why yet).
Basically, just use the "using" keyword to ensure that your writer is closed properly. The code should look something like this:
using(XmlWriter _myXmlWriter = XmlWriter.Create(outputStream, xmlWriterSettings))
{
//Build XML here
}
Upvotes: 2
Reputation: 25775
It would be helpful to identify the exact cause if you present the code you are working with. Without that, my guess is that your application leaves handles to the Filestream
open.
You should always dispose of unmanaged references using the dispose pattern (or using the using
construct).
Upvotes: 2
Reputation: 50712
use Unlocker to know which program is locking that file, and refactor the code if it is your application, you should close file after reading or writing
Upvotes: 1