.net core 如何访问copyToOutput输出的文件?

.net core 如何访问copyToOutput输出的文件?,.net-core,.net Core,我试图输出.net核心api项目中的xml文件,然后在代码中访问该文件 我在project.json中有这段代码来输出xml "buildOptions": { "emitEntryPoint": true, "copyToOutput": "myfile.xml" "preserveCompilationContext": true } 然后我试着像这样访问它 public Startup(IHostingEnvironment env) { var

我试图输出.net核心api项目中的xml文件,然后在代码中访问该文件

我在project.json中有这段代码来输出xml

"buildOptions": {
"emitEntryPoint": true,
    "copyToOutput":  "myfile.xml"
"preserveCompilationContext": true
}
然后我试着像这样访问它

    public Startup(IHostingEnvironment env)
    {
        var path = env.ContentRootPath;
        var filepath = Path.Combine(path, "myfile.xml");
        var doc = new XmlDocument();
        doc.Load(fileName); //throws error
    }
但是当我在Azure中运行这个时,我得到了这个错误

FileNotFoundException-找不到文件 “D:\home\site\wwwroot\myfile.xml”

我试过所有版本的例如,但没有运气

更新1

这是我在Program.cs中的主要方法

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
var host=new WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
.Useii整合()
.UseStartup()
.Build();
我试图通过浏览(使用Kudu服务)每个thinkable文件夹来查找xml文件,因此我认为xml文件没有生成

更新2


我发现copyToOutput在本地运行良好,但在azure上不起作用。我实现了浏览文件夹,直到我右键单击它并按publish键,我的xml文件才显示出来!这就像项目发布没有拾取它一样…

您的ContentRootPath似乎已设置为wwwroot文件夹。通常,这被设置为包含wwwroot和IHostingEnvironment的文件夹。WebRootPath指向“wwwroot”路径

检查您的程序。主类是否具有以下内容:

var host = new WebHostBuilder()
   .UseContentRoot(Directory.GetCurrentDirectory())
此外,您可能还需要将此文件添加到“publishOptions”


我不太明白你的意思:-)我更新了我的问题。可能是xml没有生成吗?好吧,我发现了“包含”,我错过了。但无论如何,我在浏览时找不到文件。我还尝试将该文件添加到另一个类库,并将其设置为copyIfNewer,但仍然找不到该文件…嗯。我又添加了一个建议,尽管从更新中可以看出你已经找到了答案。
"publishOptions": {
  "include": ["myfile.xml"]
}