开发者

File writing problem on Windows 7 Professional in c#

开发者 https://www.devze.com 2022-12-31 15:39 出处:网络
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

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.WriteEnd开发者_高级运维Element();

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


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号