gpt4 book ai didi

c# - DirectoryNotFoundException C# 将 XML 文件加载到控制台应用程序时出错

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:45 25 4
gpt4 key购买 nike

我是 C# 的新手,正在尝试解析 XML 文档,但我一直收到找不到目录的错误

显然我的 xml 文件和数据文件夹在程序启动时没有被复制到 \bin\Debug

下面是我的代码:

收到错误:

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

Additional information: Could not find a part of the path 'C:\Users\Alfred\Source\Workspaces\CIS151_MEY\AlfredM Books XML\AlfredM Books XML\bin\Debug\Data\Books.xml'.

类文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Reflection;

namespace AlfredM_Books_XML
{
class XmlHelper
{
public static XDocument GetBookDocument()
{
string appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

FileInfo asm = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

FileInfo fi = new FileInfo(asm.DirectoryName + @"\Data\Books.xml");

XDocument doc = XDocument.Load(fi.FullName);
return doc;
}
}
}

这是我添加 XML 文件的地方

enter image description here

最佳答案

Solution Explorer 中选择您的 Books.xml 文件,右键单击该文件并选择 Properties。然后将属性 Copy To Output Directory 值设置为 Copy always

关于c# - DirectoryNotFoundException C# 将 XML 文件加载到控制台应用程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com