【发布时间】:2016-01-01 23:33:25
【问题描述】:
我正在使用 ASP.NET MVC 开发一个网站。我将多行数据存储在文本行中,并将该文本文件保存在应用程序的 ContentData 文件夹中。 我遇到了这样的错误,即找不到文件路径
找不到路径“C:\Program Files (x86)\IIS”的一部分 快递\~\ContentData\WTE.txt'
请查看我的代码并帮助我执行此操作。
这是视图:
@{
ViewBag.Title = "WhatToExpect";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<h2></h2>
@{
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new
System.IO.StreamReader(@"~/ContentData/WTE.txt");
while((line = file.ReadLine()) != null)
{
string notes= line;
Html.Raw(notes);
counter++;
}
<div id="ChurchImage">
<img src="../../Images1/redeemer.jpg" alt="" />
</div>
file.Close();
}
我尝试替换文件路径中的@。没有任何效果。请帮助我 out.提前谢谢
【问题讨论】:
-
实际路径是什么?
-
我已将文本文件放在名为 ContentData 的文件夹中。该文件夹是我的应用程序中的文件夹之一。实际路径是“~/ContentData/WTE.txt”
-
尝试使用
System.IO.StreamReader(HttpContext.Server.MapPath("~/ContentData/WTE.txt"));并确保文件存在。 -
它工作#Waqar Ahmed..谢谢你。但我只能显示图像。请查看我的代码并告诉我如何显示文本?
-
我想显示文本文件中的数据。哪个 HTML 助手适合这个?
标签: c# asp.net-mvc visual-studio streamreader