【问题标题】:Access site directory path with streamreader in asp.net在 asp.net 中使用 streamreader 访问站点目录路径
【发布时间】:2011-10-20 17:42:36
【问题描述】:

从 ASP.NET 的 App_Code 文件夹中的一个类,如何访问网站根目录的路径?我试过了:

    StreamReader sr = new StreamReader("../Questions.aspx");

但它给了我程序文件中的路径...

那么我该怎么做呢?至少我认为我可以从 App_Code 文件夹导航到目录中的上层文件夹

编辑:我不是在开发一个网络应用程序,而是一个网站

【问题讨论】:

    标签: c# asp.net streamreader


    【解决方案1】:

    尝试使用

        string filePath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
        StreamReader sr = new StreamReader(filePath + @"\Questions.aspx");
    

    【讨论】:

    • 不起作用,因为正如 nsr81 所说,在“App_Code”下没有请求实例
    【解决方案2】:

    由于在“App_Code”下,您没有“HttpServerUtility”对象或“Server”变量的实例,您可以将其传递给您的函数并使用它来翻译站点路径:

    App_Code > Test.cs

    using System.Web;
    
    public class Test
    {
        public static string getfile(HttpServerUtility Server )
        {
            return Server.MapPath("~/Default.aspx");
        }
    }
    

    当您从 ASPX 页面调用它时,将其称为:

    string filepath = Test.getfile(Server);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2017-08-05
      • 2011-12-19
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      相关资源
      最近更新 更多