【问题标题】:how can I read the files copied to the output directory in ASP .net?如何读取复制到 ASP .net 中输出目录的文件?
【发布时间】:2014-02-04 20:54:15
【问题描述】:

我有一个 VS 2012 Web 服务项目,其中包含一个名为 myconfig.xml 的文件,我已将此文件的复制操作属性设置为“始终复制”

我尝试使用这一行加载 XML 文件:

doc.Load("myconfig.XML");

但是 ASP.net 引发异常,因为它无法从 C:\program files (x86)\IIS Express 7 读取文件

我试过了:

System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

但是它返回一个临时目录的路径,我怎样才能得到 myconfig.XML 的实际位置?

【问题讨论】:

    标签: c# asp.net visual-studio-2012 reflection


    【解决方案1】:

    在 ASP.NET 中,您应该始终使用 Server.MapPath

    // root folder
    var docPath = Server.MapPath("/doc.xml");
    
    // some other folder
    var docPath = Server.MapPath("/folder/doc.xml");
    

    【讨论】:

    • 就是这样,Server.MapPath 有效 :) 是否有一些方法可以返回相同的值并且我可以用于桌面和网络应用程序?
    【解决方案2】:

    应该是

    Environment.CurrentDirectory
    

    在网络环境中,您可以使用AppDomain 来找出目录,例如

    AppDomain.CurrentDomain.BaseDirectory
    

    【讨论】:

      【解决方案3】:

      您是否考虑过 asp.net 允许您创建多个配置文件?

      看看这篇文章接受的答案,它应该会为你指明正确的方向,

      Multiple ASP.NET Configuration Files

      【讨论】:

        猜你喜欢
        • 2017-03-10
        • 2017-08-09
        • 2020-04-06
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        相关资源
        最近更新 更多