简体   繁体   English

Windows 7 Professional中C#中的文件写入问题

[英]File writing problem on Windows 7 Professional in c#

I have an application in C# which I write some data to file. 我在C#中有一个应用程序,可以将一些数据写入文件。 I am facing the problem on windows 7 professional that when I write data to C:\\ProgramData , Access denied acception is thrown. 我在Windows 7 Professional上遇到的问题是,当我将数据写入C:\\ProgramData ,将引发访问拒绝接受。 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. 此问题仅在Windows 7 Professional上产生,它在Windows 7的所有其他版本以及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
}

This is normal, non-admin user accounts do not have write access to c:\\programdata. 这是正常的,非管理员用户帐户没有对c:\\ programdata的写权限。 Only Read, Execute and List privileges are assigned by default. 默认情况下,仅分配“读取”,“执行”和“列表”权限。 This includes admin accounts with UAC enabled. 这包括启用了UAC的管理员帐户。

The AppData folder should be used to write files. AppData文件夹应用于写入文件。 Get the path to that folder with Environment.GetFolderPath(). 使用Environment.GetFolderPath()获取该文件夹的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM