【发布时间】:2018-12-06 12:07:01
【问题描述】:
我正在尝试读取我的 excel CSV 文件中的特定列。因为文件已上传,所以我不想将文件路径硬编码到特定位置。当我运行代码时,出现错误“System.IO.FileNotFoundException:”找不到文件“C:\Program Files (x86)\IIS Express\North_HCA_Texts3.csv”。 该文件存在是因为我从桌面选择了它
HTML
<div class="col-md-10">
<input type="file" name="attachmentcsv" onchange="ValidateSingleInput(this);" class="form-control" id="attachmentcsv" />
</div>
C#
struct LineInMyCsvFile
{
public string MobileNumber { get; set; }
public string NAME { get; set; }
public string EMPLOYEENUMBER { get; set; }
public string JOBTITLEROLE { get; set; }
public string BAND { get; set; }
}
string filePath = string.Empty;
string path = Server.MapPath(@"~/Uploads/");
filePath = Path.GetFileName(attachmentcsv.FileName);
IEnumerable<LineInMyCsvFile> allRecords = null;
using (var reader = System.IO.File.OpenText(filePath)) // error here
{
var csvParser = new CsvParser(reader);
CsvReader r = new CsvReader(csvParser);
allRecords = r.GetRecords<LineInMyCsvFile>().ToArray();
}
foreach (var record in allRecords)
{
// Imagine you only care about the value in MobileNumber:
listmobilenumber.Add(record.MobileNumber);
}
【问题讨论】:
-
如果您从“桌面”中选择了它,那么您为什么希望它位于“程序文件”下?其次,那里有 Server.MapPath(),这是一个 Web 应用程序吗?如果是,网络用户是否有权访问该位置?
-
@CetinBasoz 是的,这是一个 Web 应用程序。我仍在使用 localhost 运行它。我从桌面选择了文件我有点困惑为什么该位置是程序文件
-
提供正确的路径。
标签: c# csv model-view-controller