c# - asp.net 获取文件的相对路径

标签 c# asp.net

我有一个 MVC4 应用程序,一个 Controller 方法需要一些文件(这个文件有一些 Json 对象),读取它处理然后删除它。

这些文件使用 ftp 上传到我的站点。我的项目结构是这样的

wwwroot
  bin
  content
  images
  scripts 
  suscribers // This is my ftp folder 
    _suscriber1
      _proccess1
        file1.txt //This is a Json file 
        file2.txt 
        ...
      _proccess2
      ...
    _suscriber2 
    ...

我的所有文件 (file1.txt...) 都加载正常。在我的 Controller 上,我试图以这种方式读取 file1.txt:

string suscriberDir= string.Format("_{0}", suscriber.Id);
string[] laPath = {System.AppDomain.CurrentDomain.BaseDirectory, "suscribers", suscriberDir};
string lcPath = Path.Combine(laPath);
string[] laPath2 = { lcPath, "_proccess1" , "_File1.txt" };
lcPath = Path.Combine(laPath2);
StreamReader reader = new StreamReader(lcPath);
string personas = reader.ReadToEnd();
reader.Close();

我的问题是它向我抛出一个 filenotfound 异常。

读取 file1.txt 并获取其内容的正确方法是什么?

最佳答案

尝试使用 Server.MapPath("_File1.txt");

关于c# - asp.net 获取文件的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126594/

相关文章:

c# - 从数据库中下载照片 C#

asp.net - Razor 与 ASPX 语法的比较

c# - 添加数据 C# 时打开 XML 不保存

c# - 将锯齿状数组转换为列表

c# - MVC 中同一接口(interface)的 CaSTLe Windsor 多重实现

javascript - 将值从代码隐藏传递给 js

javascript - 基于文本框输入的动态文本框创建

c# - 检查元素是否存在于 Selenium 中

c# - 无法加载文件或程序集 'Oracle.DataAccess 错误

javascript - 在代码后面调用 window.onload 函数不起作用