如何从StreamWriter中清除以前的行?

时间:2017-07-05 03:48:38

标签: c# streamwriter

    void BuildProjects()
    {
        String outputPath = @"D:\PIT\ProcessImprovementTool\DLL";

        console = new Process();
        console.StartInfo.FileName = "cmd.exe";
        console.StartInfo.UseShellExecute = false;
        console.StartInfo.CreateNoWindow = true;

        console.StartInfo.RedirectStandardInput = true;
        console.StartInfo.RedirectStandardOutput = true;
        console.StartInfo.RedirectStandardError = true;

        console.Exited += new EventHandler((sender, e) =>
        {
            Console.WriteLine("if (console.Exited) --> ExitCode: " + console.ExitCode);
            BuildProjects();
        });

        console.OutputDataReceived += new DataReceivedEventHandler(ConsoleOutputHandler);
        console.ErrorDataReceived += new DataReceivedEventHandler(ConsoleOutputHandler);

        console.Start();

        using (StreamWriter sw = console.StandardInput)
        {
            sw.AutoFlush = true;
            console.StandardInput.AutoFlush = true;

            if (sw.BaseStream.CanWrite)
            {


                sw.WriteLine("D:\\PIT\\ProcessImprovementTool\\callVcvars32.bat");
                sw.WriteLine("cls");

                for (int ctr = 0; ctr < 5; ctr++)
                {
                    sw.WriteLine("msbuild /property:OutputPath=" + outputPath + @";OutputType=Library " + lines[ctr]);
                    //console.BeginOutputReadLine();

                    sw.Flush();
                }


            }

            if (tryout)
            {
                Console.WriteLine("working");
            }

            sw.Close();
            //sw.Flush();
        }




        console.BeginOutputReadLine();
        console.BeginErrorReadLine();
        //console.WaitForExit();
        counter++;
    }

中的

for (int ctr = 0; ctr < 5; ctr++)
            {
                sw.WriteLine("msbuild /property:OutputPath=" + outputPath + @";OutputType=Library " + lines[ctr]);
                //console.BeginOutputReadLine();

                sw.Flush();
            }

如果我将ctr的限制设置为40(ctr <40)我的应用程序挂起..并且我得到该流已经达到其极限..

根据我的研究--Flush()应该做的伎俩。或将AutoFlush设置为True。但似乎没有用。

那么我如何清除以前的流记录..或任何让我输入超过40行的东西?...(我在streamWriter中写了100多行但不超过150行)..

“lines”变量存储“build.txt”的值

void readFromBuild()
    {
        doneReading = true;
        lines = System.IO.File.ReadAllLines(@"D:\PIT\ProcessImprovementTool\Build\build.txt");

        //System.Console.WriteLine("Contents of WriteLines2.txt = ");
        foreach (string line in lines)
        {
            lineCount++;
        }
        Console.WriteLine(lineCount);
        lineCount = lineCount - limit;
    }

“build.txt”包含要编译以生成DLL文件的所有文件。

1 个答案:

答案 0 :(得分:0)

对我而言,这就像回答一样好......我只是以另一种方式做到了......虽然它可能不那么好......感谢人们。

        buildProgress.Enabled = true;
        buildProgress.Maximum = lineCount;

        for (int ctr = 0; ctr < lineCount; ctr++)
        {

            String outputPath = @"Q:\";
            StreamReader reader;
            String outputLine;
            console = new Process();
            console.StartInfo.FileName = "cmd.exe";
            console.StartInfo.UseShellExecute = false;
            console.StartInfo.CreateNoWindow = true;

            console.StartInfo.RedirectStandardInput = true;
            console.StartInfo.RedirectStandardOutput = true;
            console.StartInfo.RedirectStandardError = true;

            console.Start();

            using (StreamWriter sw = console.StandardInput)
            {
                sw.AutoFlush = true;
                console.StandardInput.AutoFlush = true;

                if (sw.BaseStream.CanWrite)
                {
                    sw.WriteLine(@"Q:\");       
                    sw.WriteLine("msbuild /property:OutputPath=" + outputPath + @";OutputType=Library " + lines[ctr]);
                    sw.Flush();
                }
                sw.Close();
            }

            reader = console.StandardOutput;
            outputLine = reader.ReadToEnd();
            console.WaitForExit();
            //if (console.HasExited)
           // {

                Console.Write(outputLine);
                //outputTextBox.Text += outputLine;

                pathToLog = @"Q:\" + fileList[ctr];

                File.Create(pathToLog).Dispose();
                File.WriteAllText(pathToLog, outputLine);

                buildProgress.Value = ctr;

                string[] readThatLog = File.ReadAllLines(pathToLog);

                foreach(string line in readThatLog)
                {
                    if (line == "Build succeeded.")
                    {

                        outputTextBox.AppendText(lines[ctr]);
                        outputTextBox.AppendText(Environment.NewLine + line);
                        outputTextBox.AppendText(Environment.NewLine + Environment.NewLine);

                    }
                    else if (line == "Build FAILED.")
                    {

                        outputTextBox.AppendText(lines[ctr]);
                        outputTextBox.AppendText(Environment.NewLine + line);
                        outputTextBox.AppendText(Environment.NewLine + Environment.NewLine);
                    }
                }
           // }
        }

        buildProgress.Value = 0;
        buildProgress.Enabled = false;
        Console.WriteLine("\n\n\n\nDONE!!!");
    }