简体   繁体   English

拒绝访问XML文件的路径

[英]Access to path of XML file is denied

So I'm currently working on an ASP.NET project. 因此,我目前正在研究ASP.NET项目。 It requires me to write a List<> onto a XML file. 它要求我将List <>写入XML文件。 I created a "data.xml" file within the project folder and here are my few lines of code: 我在项目文件夹中创建了一个“ data.xml”文件,这是我的几行代码:

XmlSerializer serializer = new XmlSerializer(typeof(List<BookItem>));
TextWriter filestream = new StreamWriter("data.xml");
serializer.Serialize(filestream, book);
filestream.Close();

Everything runs smoothly until the "write-data-to-xml-file" part. 一切运行顺利,直到“将数据写入xml文件”部分为止。 I encountered this issue: 我遇到了这个问题:

System.UnauthorizedAccessException: 'Access to the path 'C:\Program Files (x86)\IIS Express\data.xml' is denied.'

Am I missing any setting or should I include any tricky line of code? 我是否缺少任何设置还是应该包含任何棘手的代码行?

Maybe you have trouble overwriting the .xml. 也许您无法覆盖.xml。 Is it open somewhere, eg your IDE? 它是否在某个地方(例如您的IDE)打开? If not, do you have permission to overwrite it? 如果没有,您是否有权覆盖它? Try deleting it and see if your code executes. 尝试删除它,看看您的代码是否执行。

you need to do two things 你需要做两件事

1- go to your file and change security permission and allow full control, modify, read, and write. 1-转到您的文件并更改安全权限,并允许完全控制,修改,读取和写入。

2- go to task manager and end data.xml . 2-转到任务管理器并结束data.xml

Change this line 更改此行

TextWriter filestream = new StreamWriter("data.xml"); 

into this 进入这个

 TextWriter filestream = new StreamWriter(@"fullpath");

尝试放置特定的文件位置,例如c:\\ data.xml,因为我认为原因是因为它无法写入默认位置。

TextWriter filestream = new StreamWriter(@"c:\data.xml");

Ok, some small problems 好,有一些小问题

new StreamWriter("data.xml");

you have not specified full path and IIS try to create in relative path that is run and your IIS user has not sufficient privileges to create this file or modify existing file with the same name. 您尚未指定完整路径,并且IIS尝试在运行的相对路径中创建,并且IIS用户没有足够的权限来创建此文件或修改具有相同名称的现有文件。 you can 您可以

Server.Map("~")

to get your running website path and create a custom path for creating your files 获取正在运行的网站路径并创建用于创建文件的自定义路径

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

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