namespace qarfiles
{
    class Program
    {
        static void Main(string[] args)
        {
           //变量和list的定义这里省略,否则代码粘贴不下
            string folderpath = @"D:\Mainline_March\";            DirectoryInfo TheFolder = new DirectoryInfo(folderpath);            foreach (FileInfo NextFile in TheFolder.GetFiles())
            {
                if (NextFile.Extension.ToUpper() != @".SH")
                {                    continue;
                }                StreamReader sr = new StreamReader(folderpath + NextFile.Name);
                while ((line = sr.ReadLine()) != null)
                {
                    string str = line;
                    if (line.Contains(@"./xcode.configGenericVideoEnc.pl"))
                    {                        string[] ss = str.Split(' ');
                        for (int i = 0; i < ss.Length; i++)
                        {
                            if (ss[i] == "-u32PID")
                            {
                                pid = ss[i + 1];
                                pids.Add(pid);                            }                            if (ss[i] == "-u32EncoderType")
                            {
                                encodertype = ss[i + 1];
                                int encod = int.Parse(encodertype);
                                encodertypes.Add(encod);                            }                            if (ss[i] == "-u32CodingMode")
                            {
                                codingmode = ss[i + 1];
                                int cod = int.Parse(codingmode);
                                codingmodes.Add(cod);                            }                        }
                    }                    if (line.Contains(@"-width"))
                    {                        string[] sss = str.Split(' ');
                        for (int j = 0; j < sss.Length; j++)
                        {
                            if (sss[j] == "-width")
                            {
                                width = sss[j + 1];
                                widths.Add(width);
                            }                            if (sss[j] == "-height")
                            {
                                height = sss[j + 1];
                                heights.Add(height);                            }                            if (sss[j] == "-frmrate")
                            {
                                frmrate = sss[j + 1];
                                int frm = int.Parse(frmrate);
                                frmrates.Add(frm);                            }
                            if (sss[j] == "-vidformat")
                            {
                                vidformat = sss[j + 1];
                                int vid = int.Parse(vidformat);
                                vidformats.Add(vid);                            }                        }
                    }
                    if (line.Contains(@"./createLogo.pl"))
                    {                        string[] ssss = str.Split(' ');
                        for (z = 0; z < ssss.Length; z++)
                        {
                            if (ssss[z] == "-f")
                            {
                                figure = ssss[z + 1];
                                figures.Add(figure);
                            }                        }                    }
                }
                sr.Close();
                string figg = figures[0].Substring(30, figures[0].Length - 30);
                string ET = "";
                for (n = 0; n < codingmodes.Count; n++)
                {
                    if (codingmodes[n] == 1)
                    {
                        codds.Add("FRAME");
                    }
                    else
                    {
                        codds.Add("FIELD");
                    }
                }
                double rate = 0.0;
                if (encodertypes[0] == 0)
                {
                    ET = "H264";
                }
                else
                {
                    ET = "MPEG2";
                }                int outerlace = vidformats[0];                for (w = 0; w < frmrates.Count; w++)
                {
                    switch (frmrates[w])
                    {
                        case 1:
                            rates.Add("23.97");
                            break;
                        case 2:
                            rates.Add("24");
                            break;
                        case 3:
                            rates.Add("25");
                            break;
                        case 4:
                            rates.Add("29.97");
                            break;
                        case 5:
                            rates.Add("30");
                            break;
                        case 6:
                            rates.Add("50");
                            break;
                        case 7:
                            rates.Add("59.94");
                            break;
                        case 8:
                            rates.Add("60");
                            break;
                        case 11:
                            rates.Add("12");
                            break;
                        case 12:
                            rates.Add("15");
                            break;
                        case 13:
                            rates.Add("10");
                            break;
                        case 14:
                            rates.Add("9.99");
                            break;
                        case 15:
                            rates.Add("11.99");
                            break;
                        case 16:
                            rates.Add("12.5");
                            break;
                        case 17:
                            rates.Add("14.99");
                            break;
                    }
                }
                    string path = (folderpath + NextFile.Name).Replace(@".sh", @".qar");
                    StreamWriter sw = new StreamWriter(path, true);
                    StringBuilder qarContent = new StringBuilder();
                    qarContent.Append("Results StartCapture " + "tsName" + "\n");
                    qarContent.Append("Results SetVideoInput " + " " + "inputStreamName" + " " + "inputStreamWidth" + " " + "inputStreamHeight" + " " + "inputInterlace" + " " + "inputfrmrate" + " " + "inputEncodeType" + "\n");
                    qarContent.Append("Results SetVideoOnput " + " " + "tsName" + " " + widths[0] + " " + heights[0] + " " + outerlace + " " + rates[0] + " " + ET + "\n")
                    qarContent.Append("\n");
                    qarContent.Append("SSH ExecRead \"chmod 777 /tmp \"" + "\n")
                    qarContent.Append("SSH Upload \"" + figg + "\"" + " " + "\"/tmp/" + figg + "\"\n");
                    qarContent.Append("SSH Upload \"" + "shName" + "\" \"/tmp/" + "shName" + "\"\n");
                    qarContent.Append("SSH ExecRead \"dos2unix /tmp/" + "shName" + "\"" + "\n");
                    qarContent.Append("\n")
                    qarContent.Append("**PITCH** Play **SOURCEDIR** " + "\"" + "\\" + "inputStreamName" + "\"" + " " + "inputStreamBitrate" + "\n");
                    qarContent.Append("SSH ExecRead \"cd /tmp;" + "sh" + " " + "shName" + "\"" + "\n");                    qarContent.Append("\n")
                    qarContent.Append("**CATCH** Record **CAPTUREDDIR** " + "\"" + "\\" + "inputStreamName" + "\" 00:00:45" + "\n");
                    qarContent.Append("**CATCH** Stop\n");
                    qarContent.Append("**PITCH** Stop\n");
                    qarContent.Append("\n");
                    qarContent.Append("ThrottleCheck.qac" + "\n");
                    qarContent.Append("qaverify Compare Clear" + "\n");
                    for (k = 0; k < pids.Count; k++)                    {
                        qarContent.Append("qaverify Compare vFormat equ" + " " + ET + " " + "in" + " " + pids[k] + "\n");  //有多少个pid循环几次    
                        qarContent.Append("qaverify Compare vHorizontalRes equ " + widths[k] + " " + pids[k] + "\n");
                        qarContent.Append("qaverify Compare vVerticalRes equ " + heights[k] + " " + pids[k] + "\n");
                        qarContent.Append("qaverify Compare vFrameRate equ" + " " + rates[k] + " " + pids[k] + "\n");
                       qarContent.Append("qaverify Compare vEncodeMode equ" + " " + codds[k] + " " + pids[k] + "\n");
                    }
                    qarContent.Append("\n");
                    qarContent.Append("qaverify Compare ETR290 equ 0" + "\n");
                    qarContent.Append("qaverify Compare VBVOverflow equ 0" + "\n");
                    qarContent.Append("qaverify Compare VBVUnderflow equ 0" + "\n");
                    qarContent.Append("qaverify Analyze **CAPTURE** + \"\\" + "tsName" + "\"" + "\n");
                    qarContent.Append("Results EndCapture" + "\n");
                    qarContent.Append("Results SaveCapture \"Result.csv\"" + "\n");
                    sw.Write(qarContent + "\n");
                    sw.Flush();
                    sw.Close();                           } 
            }
         }   
      }程序没加上遍历目录的时候可以一对一地生成,可是加上遍历目录后,影响了qarContent的循环,见标红处,菜鸟求指点解决方法
c#