【发布时间】:2017-02-18 13:38:59
【问题描述】:
您好,我正在编写一个 c# 代码,其中有一个字符串作为参数输入发送到该方法。然后必须在文件中搜索 inputString 并返回结果。目前我知道如何以常规方式执行此操作(使用文件 IO)。
[HttpPost]
public string UsernameValidation(string username)
{
string text = username;
string userExists = usernameNotAvailable;
string line;
System.IO.StreamReader file = new System.IO.StreamReader("~/UserData/usernameslist.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
userExists = usernameAvailable;
}
}
return userExists;
}
但这是一个转折点,我的项目是在 MVC 中。我可以使用string userDataFile = Server.MapPath("~/UserData/usernameslist.txt"); 获取文件的路径。
但我不知道如何获得在文件中搜索字符串的功能。
请告诉我该怎么做。
谢谢
【问题讨论】:
标签: c# asp.net-mvc-5 asp.net-mvc-controller