views:

44

answers:

1

I have an application in C# which I write some data to file. I am facing the problem on windows 7 professional that when I write data to C:\ProgramData, Access denied acception is thrown. If I login from an administrator account this issue vanishes, and if I login from some other account who have administrative previlages this issue comes up. This issue is only produces on windows 7 professional, it is working fine on all other flavors of windows 7 as well as windows vista.

try
{
XmlTextWriter myXmlTextWriter = new XmlTextWriter("Configuration.xml", null);
            myXmlTextWriter.Formatting = Formatting.Indented;
            myXmlTextWriter.WriteStartDocument(true);
            myXmlTextWriter.WriteDocType("ApplicationConfigurations", null, null, null);
            ////myXmlTextWriter.WriteComment("This file represents another fragment of a book store inventory database");
            myXmlTextWriter.WriteStartElement("Configuration");
            myXmlTextWriter.WriteElementString("firstElement",  pe.ToString());
            myXmlTextWriter.WriteEndElement();

            myXmlTextWriter.WriteEndDocument();
            myXmlTextWriter.Flush();
            myXmlTextWriter.Close();
}catch(Exception e)
{
    //Exception is thrown in Win7 professional
}
+3  A: 

This is normal, non-admin user accounts do not have write access to c:\programdata. Only Read, Execute and List privileges are assigned by default. This includes admin accounts with UAC enabled.

The AppData folder should be used to write files. Get the path to that folder with Environment.GetFolderPath().

Hans Passant
thanks Hans for your help.
Ummar