我尝试逐行从文本文件中读取一个字符串,但是我收到此错误,请参阅我的代码。还有另一种逐行读取字符串的方法,或者这个错误的一种解决方案?该文件以UTF8保存。
文件内容链接:https://shrib.com/#MHVN4JhCgKStecY7Q4ei
public Games RetornarGames(string arquivo)
{
try
{
Game game = new Game();
Games games = new Games();
foreach (string linha in File.ReadAllLines(arquivo, Encoding.UTF8))
{
Match action = regexAction.Match(linha);
switch (action.Value)
{
case "InitGame":
game = InitGame();
games.ListGames.Add(game);
break;
case "ClientConnect":
ClientConnect(game, linha);
break;
case "ClientUserinfoChanged":
ClientUserInfoChanged(game, linha);
break;
case "Kill":
Kill(game, linha);
break;
default:
break;
}
}
return games;
}
catch (Exception ex)
{
throw;
}
}
答案 0 :(得分:0)
我为StringReader更改了File.ReadLine并且正常工作,谢谢!
StringReader sr = new StringReader(arquivo);
while (true)
{
string linha = sr.ReadLine();
if (linha != null)
{
Match action = regexAction.Match(linha);
switch (action.Value)
{
case "InitGame":
game = InitGame();
games.ListGames.Add(game);
break;
case "ClientConnect":
ClientConnect(game, linha);
break;
case "ClientUserinfoChanged":
ClientUserInfoChanged(game, linha);
break;
case "Kill":
Kill(game, linha);
break;
default:
break;
}
}
else
{
break;
}
}
return games;
}
catch (Exception ex)
{
throw;
}
}