Deny access to files in .NET.

I am writing an application in C # that saves data in xml. When I open the application again, it says

Access to path "C: \ ProgramData \ Test \ abc.xml" was denied.

Can someone help me deal with this issue.

Is there a way to find why access is denied.

+4
source share
4 answers

You are probably getting this error because you are using Vista or Win7 and you have UAC enabled.

C: \ ProgramData requires admin priveledges to be written (but not read).

I just found this because of difficulties because I was working on a program that worked fine on XP, which used System.Environment.SpecialFolder.CommonApplicationData, which in Vista refers to the above location and requires that the side sheets write this directory.

+4
source

It would be useful to determine the exact cause if you present the code you are working with. Without this, I assume that your application leaves Filestream open handles.

You should always manage unmanaged links using the dispose pattern (or using using construct ).

+2
source

This is a common problem that I have encountered with an XML author in the past. If the used Filestream is not closed properly, the file will remain locked. I say this is a problem, mainly because I noticed that for some reason the lock remains even after the application is closed, but I saw how it happens when I use the XML writer in this way (I don’t know why else).

Basically, just use the "using" keyword to make sure your writer is properly closed. The code should look something like this:

 using(XmlWriter _myXmlWriter = XmlWriter.Create(outputStream, xmlWriterSettings)) { //Build XML here } 
+2
source

use Unlocker to find out which program is blocking this file, and to reorganize the code, if this is your application, you must close the file after reading or writing

+1
source

All Articles