无法绕过我的相对路径 - c#

这可能是令人难以置信的显而易见的事情,但是我是C#的新手,所以要格外温柔...

我有一个应用程序(理论上)将文本文件解析为数组。尽管文本文件是aspx文件的同级文件,但我无法正确获得相对路径。不知道它是否有任何区别(我想没有),但是我正在使用代码隐藏功能。

我的文件夹结构如下所示:

default.aspx
default.aspx.cs
default.aspx.designer.cs
album.cs
albums.txt
web.config

这是我正在使用的代码:

 protected void Page_Load(object sender, EventArgs e)
    {

         string[] allLines = File.ReadAllLines(@"Albums.txt");
         Album[] Albums = new Album[allLines.Length];
         for (int i = 0; i < allLines.Length; i++)
         {
           string[] lineSplit = allLines[i].Split(',');
           Albums[i] = new Album();
           Albums[i].ID = Convert.ToInt32(lineSplit[0]);
           Albums[i].title = lineSplit[1];
           Albums[i].keyName = lineSplit[2];
       }
   }

但是,在构建它时,出现错误,提示无法找到albums.txt,并且失败。

任何指针将不胜感激。

参考方案

Server.MapPath指定要映射到物理目录的相对或虚拟路径。

* Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
* Server.MapPath("..") returns the parent directory
* Server.MapPath("~") returns the physical path to the root of the application
* Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

一个例子:

假设您将网站应用程序(http://www.example.com/)指向了

C:\ Inetpub \ wwwroot

并在以下位置安装您的商店应用程序(IIS中的子网站为虚拟目录,标记为应用程序)

D:\ WebApps \ shop

例如,如果您在以下请求中调用Server.MapPath:

http://www.example.com/shop/product/GetProduct.aspx?id=2342

然后,

* Server.MapPath(".") returns D:\WebApps\shop\products
* Server.MapPath("..") returns D:\WebApps\shop
* Server.MapPath("~") returns D:\WebApps\shop
* Server.MapPath("/") returns C:\Inetpub\wwwroot
* Server.MapPath("/shop") returns D:\WebApps\shop

如果Path以正斜杠(/)或反斜杠()开头,则MapPath方法将返回路径,就好像Path是完整的虚拟路径一样。

如果Path不以斜杠开头,则MapPath方法返回相对于正在处理的请求目录的路径。

注意:在C#中,@是原义文字字符串运算符,表示该字符串应“按原样”使用,而不对转义序列进行处理。

Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\"), Server.MapPath("/"). What is the difference?

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

每个文件合并后添加换行 - python

我有很多类似以下内容的JSON文件:例如。1.json{"name": "one", "description": "testDescription...", "comment": ""} test.json{"name"…

Json到php,json_decode返回NULL - php

我正在用PHP进行JSON解析器的一些API,用于存储有关遗产的信息。我在解析时遇到问题,因为它返回的是NULL值而不是数组或对象。简单的JSON代码可以很好地解析,但是可以这样:{"success":true,"totalCount":1,"data":[{"id":99694…

将ajax的值存储到javascript变量中 - javascript

我有一个php文件,其中我从服务器获取数据。该php文件的输出是一个包含json格式数据的变量。PHP文件:<?php $dbHostName = "localhost"; $dbUserName = "venseld"; $dbUserPass = "wecuuu"; $dbName = &…