Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用多行时,C#中的字符串格式设置速度较慢_C#_String_Parsing_Templates - Fatal编程技术网

使用多行时,C#中的字符串格式设置速度较慢

使用多行时,C#中的字符串格式设置速度较慢,c#,string,parsing,templates,C#,String,Parsing,Templates,我创建了一个进程,该进程读取一个“模板”文本文件,然后基于字符串。格式要求使用标记将自定义文本放入其中 所以,一切都是可行的,但过程是缓慢的 模板文件可以有大约500-1000行;我正在寻找加快这一进程的方法 有什么想法吗 下面是我的代码: templateFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(

我创建了一个进程,该进程读取一个“模板”文本文件,然后基于字符串。格式要求使用标记将自定义文本放入其中

所以,一切都是可行的,但过程是缓慢的

模板文件可以有大约500-1000行;我正在寻找加快这一进程的方法

有什么想法吗

下面是我的代码:

templateFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace("file:\\", "");
templateFilePath += "\\Templates\\TemplateFile.txt";
tempRequestFilePath = System.IO.Path.GetTempPath();
tempRequestFilePath += Guid.NewGuid();
Directory.CreateDirectory(tempRequestFilePath);
responseFileToWrite = tempRequestFilePath + "\\" + Path.GetFileNameWithoutExtension(zipMergeFilePath) + ".RSP";
if (!File.Exists(templateFilePath))
{
    return false;
}
templateString = System.IO.File.ReadAllText(templateFilePath);
currentRecordNumber = 1;
for (int i = 0; i < createToProcess.rtfText.Lines.Length; i++)
{
    if (createToProcess.rtfText.Lines[i].Contains("TAG ID:"))
    {
        string currentTagID = createToProcess.rtfText.Lines[i].Substring(9, 11).Trim();
        string currentCustomerNumber = createToProcess.rtfText.Lines[i].Substring(25, 12).Trim();
        string currentTaxPeriod = createToProcess.rtfText.Lines[i].Substring(42, 8).Trim();
        string currentCustomerPhoneNumber = createToProcess.rtfText.Lines[i].Substring(55, 9).Trim();
        DateTime datePurchases = (DateTime.Now).AddDays(-7);
        DateTime dateReceived = (DateTime.Now).AddYears(10);
        DateTime dateModified = (DateTime.Now).AddYears(-1);
        string currentResearchCreateRecord = String.Format(templateString,
            currentTagID.PadRight(6),
            currentCustomerNumber.PadRight(12),
            currentTaxPeriod.PadRight(6),
            currentCustomerPhoneNumber.PadRight(8),
            datePurchases.Month.ToString("00") + datePurchases.Day.ToString("00") + datePurchases.Year.ToString("0000"),
            "RecordNo: " + currentRecordNumber.ToString(),
            dateReceived.Month.ToString("00") + dateReceived.Day.ToString("00") + dateReceived.Year.ToString("0000"),
            dateModified.Month.ToString("00") + dateModified.Day.ToString("00") + dateModified.Year.ToString("0000")
            );
        System.Windows.Forms.Application.DoEvents();
        File.AppendAllText(responseFileToWrite, currentResearchCreateRecord);
        currentRecordNumber += 1;
    }
}
using (ZipFile currentZipFile = new ZipFile())
{
    currentZipFile.AddFile(responseFileToWrite, "");
    currentZipFile.Save(zipMergeFilePath);
}
return true;
templateFilePath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().GetName().CodeBase)。替换(“文件:\\”,“”);
templateFilePath+=“\\Templates\\TemplateFile.txt”;
tempRequestFilePath=System.IO.Path.GetTempPath();
tempRequestFilePath+=Guid.NewGuid();
CreateDirectory(tempRequestFilePath);
responseFileToWrite=tempRequestFilePath+“\\”+Path.GetFileNameWithoutExtension(zipMergeFilePath)+“.RSP”;
如果(!File.Exists(templateFilePath))
{
返回false;
}
templateString=System.IO.File.ReadAllText(templateFilePath);
currentRecordNumber=1;
对于(int i=0;i
您正在打开每行的文件句柄。这是一个昂贵的手术,而且会让你慢下来

相反,为文件创建(在
中使用
块)一个
StreamWriter
,并调用
WriteLine()
写入一行而不关闭文件


此外,读取
属性也很重要。将其更改为
foreach
循环(或缓存数组),而不是为每行重新运行所有代码

最后,不要调用
DoEvents()

小心使用“+”运算符,它们非常慢

您应该使用“StringBuilder”操作符

System.Text.StringBuilder sb=new System.Text.StringBuilder((int)(sLen*Loops*1.1));

对于(i=0;i
createToProcess.rtfText.Lines
肯定会被调用很多。这是不是每次调用时都会将字符串分成几行?你是对的;我将放入临时数组。感谢这些信息;如果一切顺利,我会立即尝试,然后标记为答案。在
StreamWriter.WriteLine()上快速提问
part;正如您在上面的示例中所注意到的那样,我提取了整个模板文本,然后每次都会写入整个模板文本。
StreamWriter.WriteLine()
在这种情况下,是否可以将多行文本写入到文本中?回答我自己的问题,
StreamWriter.WriteLine()
确实写了多行。@ChrisK:是;
只是意味着它添加了一个新行。
System.Text.StringBuilder sb = new System.Text.StringBuilder((int)(sLen * Loops * 1.1));
for(i=0;i<Loops;i++) sb.Append(sSource);
sDest = sb.ToString();