如何提高streamwriter的性能

时间:2014-02-06 04:42:06

标签: c# .net

我正在使用紧凑型框架3.0开发智能设备 我正在使用streamwriter类在文件上写入数据。我的文件大小为256KB,但保存文件需要大约60秒。

这是我的代码:

using (StreamWriter sw = new StreamWriter(tmppath, true))
{
    sw.AutoFlush = true;

    sw.WriteLine(_RM.GetString("Key"));

    sw.WriteLine(RM.GetString("Key1") + "\t" + Controller.Time.ToString("yyyy-MM-ddHH:mm:ss"));

    sw.WriteLine(RM.GetString("Name") + "\t" + Controller.SystemName);
    sw.WriteLine(RM.GetString("SystemName") + "\t" + stCtrlName);
    sw.WriteLine(RM.GetString("SystemId") + "\t" + stCtrlId);

    sw.WriteLine();

    Controller.GetVersion(out major, out minor, out revision, out build);
    sw.WriteLine(RM.GetString("build") + string.Format("\t{0}.{1}.{2}", major, minor, build));

    sw.WriteLine(OptionsSection(SystemInfo.ModuleType.Control));
    sw.WriteLine();


    sw.WriteLine(OptionsSection(SystemInfo.ModuleType.Drive));
    sw.WriteLine();


    sw.WriteLine(OptionsSection1());


    string header = FormatMsg(stSeqNo, stType, stDomain, stId, stTitle, stDate, RM.GetString("Key2"), stArgs);

    using (Log evLog = new Log(Log.DomainType.All))
    {
        sw.WriteLine();

        // {0} log messages
        string domain = string.Format(RM.GetString("STBEVT_TXT_LOGFILENEWDOMAIN"), Log.GetEventLogDescription(Log.DomainType.All));
        sw.WriteLine(domain);

        // Write column headings
        sw.WriteLine(header);

        string str = string.Empty;

        foreach (EventMessage em in evLog.GetEvents())
        {
            sw.WriteLine(FormatMsg(em.SequenceNumber, em));
        }
    }

    Log.DomainType domType = Log.DomainType.NoOfDomains;
    foreach (EventMessage em in allEvents)
    {
        if (_progressBar._pBar.Value >= _progressBar._pBar.Maximum)
        {
            _progressBar._pBar.Value = 0;
        }
        else
        {

            _progressBar._pBar.Value += 1;
        }
        if (em.Domain != domType)
        {
            sw.WriteLine();

            // {0} log messages
            string domain = string.Format(RM.GetString("STBEVT_TXT_LOGFILENEWDOMAIN"), Log.GetLogDescription(em.Domain));
            sw.WriteLine(domain);

            // Write column headings
            sw.WriteLine(header);

            domType = em.Domain;
        }

        sw.WriteLine(FormatMsg(em.SequenceNumber, em));

    }

请告诉我如何提高性能,以节省256KB数据需要10到15秒。

0 个答案:

没有答案