简体   繁体   English

如何读取项目根目录中的.txt文件?

[英]How can I read .txt file in my project root?

I have a project in MVC and I want to read a .txt file in the project root. 我在MVC中有一个项目,我想在项目根目录中读取.txt文件。

Because I will send this solution to my partner and he will just click run. 因为我会将这个解决方案发送给我的合作伙伴,所以他只需单击“运行”即可。 I have tried to use Combine code but I have an error like "path does not exist". 我尝试使用Combine代码,但是出现类似“路径不存在”的错误。

var path = Path.Combine(Directory.GetCurrentDirectory(), @"\CustomerFile-2015-07-30T1510.txt");
System.IO.StreamReader file = new System.IO.StreamReader(path);

Since it is an ASP.NET MVC project, I would suggest to use Server.MapPath("~") to get the root of your ASP.NET Application. 由于它是一个ASP.NET MVC项目,因此建议使用Server.MapPath("~")来获取ASP.NET应用程序的根。

var root = Server.MapPath("~");
var path = Path.Combine(root, @"CustomerFile-2015-07-30T1510.txt");
System.IO.StreamReader file = new System.IO.StreamReader(path);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM