LineReader 内存不足

Out of memory in LineReader

我正在制作一个读取 .tyd 文件并尝试将 " 之间的所有文本从英语翻译成意大利语的程序。

GoToDesk 翻译->“寻找电脑”

问题是我仍然“内​​存不足”。

密码是:

using System.Collections;
using System.Net;
using MiscUtil.IO;

namespace Soft_inc
{
 class MyProject
{
    public static string TranslateText(string input, string languagePair)
    {
        string url = String.Format("http://translate.google.it/?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
        WebClient webClient = new WebClient();
        webClient.Encoding = System.Text.Encoding.UTF8;
        string result = webClient.DownloadString(url);
        result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
        result = result.Substring(result.IndexOf(">") + 1);
        result = result.Substring(0, result.IndexOf("</span>"));
        return result.Trim();
}
       public static void Main()
    {
        string path_file = @"H:\Games\Software.In.v11.7.62\Software.In.v11.7.62\Localization\Italiano\idk\UI.tyd";

        string Ftext = System.IO.File.ReadAllText(path_file);
        ArrayList ar = new ArrayList();
    Console.WriteLine("This may require some time.");
    foreach (string line in new LineReader(() => new StringReader(Ftext)))
    {
        if(line.IndexOf("\"") == -1) continue;
        string text = line.Substring(line.IndexOf("\""));
        text = text.Replace("\"","");

        if(text.Length == 0) continue;
        ar.Add(text);
    }

    int idk = 0;

    while(true)
    {
        idk++;
        if(idk == ar.Count) break;
        string oldT = (string)ar[idk];
        Ftext = Ftext.Replace(oldT, TranslateText(oldT,"en|it"));
    }


    System.IO.File.WriteAllText("UI.tyd",Ftext);
}
}


}

可能是因为文件有2535行文本? 我该如何解决这个问题?

您需要使用 StreamReader class. It is not necessary to read all file content into RAM. Open one StreamReader and one StreamWriter. Reed file line by line and write translated data into a temporary file. When all content is translated just move 临时文件到所需的目的地。移动前不要忘记关闭源句柄和目标句柄。