Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net MVC中读取文本文件时出错_C#_Asp.net Mvc_Visual Studio_Streamreader - Fatal编程技术网

C# 在asp.net MVC中读取文本文件时出错

C# 在asp.net MVC中读取文本文件时出错,c#,asp.net-mvc,visual-studio,streamreader,C#,Asp.net Mvc,Visual Studio,Streamreader,我正在使用ASP.NETMVC开发一个网站。我将多行数据存储在文本行中,并将该文本文件保存在应用程序的ContentData文件夹中。 我遇到了这样的错误:找不到文件路径 找不到路径“C:\Program Files(x86)\IIS”的一部分 Express\~\ContentData\WTE.txt' 请看我的代码,并帮助我如何做到这一点 这是一种观点: @{ ViewBag.Title = "WhatToExpect"; Layout = "~/Views/Shared/

我正在使用ASP.NETMVC开发一个网站。我将多行数据存储在文本行中,并将该文本文件保存在应用程序的ContentData文件夹中。 我遇到了这样的错误:找不到文件路径

找不到路径“C:\Program Files(x86)\IIS”的一部分 Express\~\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();
    }
@{
ViewBag.Title=“WhatToExpect”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@{
int计数器=0;
弦线;
//读取文件并逐行显示。
System.IO.StreamReader文件=新建
System.IO.StreamReader(@“~/ContentData/WTE.txt”);
而((line=file.ReadLine())!=null)
{
弦乐=行;
Html.Raw(notes);
计数器++;
}
file.Close();
}
我已尝试替换文件路径中的@。没用,请帮帮我
退出。提前感谢

将服务器映射路径添加到您的路径

System.IO.StreamReader(@Server.MapPath("~/ContentData/WTE.txt"));
编辑

你的控制器应该是

public ActionResult Index()
{
    var root = AppDomain.CurrentDomain.BaseDirectory;
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader( root + @"/Content/Site.css");
    var fileLines = new List<string>();
    while ((line = file.ReadLine()) != null)
    {
        fileLines.Add(line);
    }
    // You can use model, temdata or viewbag to carry your data to view.
    ViewBag.File = fileLines;
    return View();
}

实际路径是什么?我已将文本文件放在名为ContentData的文件夹中。该文件夹是我的应用程序中的一个文件夹。实际路径是“~/ContentData/WTE.txt”,请尝试使用
System.IO.StreamReader(HttpContext.Server.MapPath(“~/ContentData/WTE.txt”)并确保文件在那里。它可以工作#Waqar Ahmed..谢谢。但我只能显示图像。请查看我的代码并告诉我如何显示文本?我想显示文本文件中的数据。哪个HTML帮助程序适用于此?我想显示文本文件中的数据。哪个HTML帮助程序适用于此?@sujaykodamala,您的数据的性质是什么?您希望如何在应用程序中使用此数据?我的意思是只显示每一行,或者你想提取每一个单词,或者做一些逻辑工作?我只想显示文本文件中的数据。这对@Abishek有帮助吗?这是一个多行文本。@sujaykodamala,我想你已经在代码中这样做了。正确的?通过使用``while`循环。我想显示文本文件中的数据。哪个HTML助手适用于此?
public ActionResult Index()
{
    var root = AppDomain.CurrentDomain.BaseDirectory;
    string line;
    System.IO.StreamReader file = new System.IO.StreamReader( root + @"/Content/Site.css");
    var fileLines = new List<string>();
    while ((line = file.ReadLine()) != null)
    {
        fileLines.Add(line);
    }
    // You can use model, temdata or viewbag to carry your data to view.
    ViewBag.File = fileLines;
    return View();
}
@{
    string line;
    foreach (var item in ViewBag.File)
    {
        <p>@item</p>
    }
}