C# “意外”&引用;从StreamReader到StreamWriter

C# “意外”&引用;从StreamReader到StreamWriter,c#,logic,C#,Logic,我试过很多事情,也做过调查,问过朋友,但似乎写第二行的时候都会用“替换”这个词。我所要做的就是在一个单独的文件中为每个条目读取一行 每个读取文件都有以下几个项目: 2/20/2014 7:33:10 AM OPERATOR: jason FILE: C:\ax14\Setups\000062363106RH.prt UNITS: english TEST RESULT: Pass CHANNEL 1 TEST TYPE: VELOCITY RESULT:

我试过很多事情,也做过调查,问过朋友,但似乎写第二行的时候都会用“替换”这个词。我所要做的就是在一个单独的文件中为每个条目读取一行

每个读取文件都有以下几个项目:

2/20/2014 7:33:10 AM
OPERATOR: jason
FILE: C:\ax14\Setups\000062363106RH.prt
UNITS: english
TEST RESULT: Pass

    CHANNEL 1
        TEST TYPE: VELOCITY
        RESULT:  Pass
        UPPER LIMIT: 0.2260
        LOWER LIMIT: 0.2220
        MAX THICKNESS: 2.0110
        MIN THICKNESS: 1.0110
        MEASURED VELOCITY: 0.2225
        MEASURED THICKNESS: 1.5215
我希望将日期和速度线放在一行中,如下所示: “2014年2月20日上午7:33:10,实测速度:0.2225”

这就是我的问题

2/20/2014 7:33:10 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2225
,
2/20/2014 7:52:28 AM,
,
,
,
,
,
,
,
,
,
,
,
,   MEASURED VELOCITY: 0.2224
,
2/20/2014 7:58:46 AM,


只有同时具有以下两个值时,才写入该行: 然后将两个值重置为null

sw.WriteLine(x[0] + "," + x[1]);
变成:

if ( !String.IsNullOrWhitespace( date) && !String.IsNullOrWhitespace( velocity )
{
  sw.WriteLine(x[0] + "," + x[1]);
  date = null;
  velocity = null;
}
正如Blas所提到的,您还需要将变量移到while语句之外:

String result = "";
String velocity = "";
while ((line = sr.ReadLine()) != null)

您可以使用此正则表达式模式来实现您的目标:

(^.*?(?:AM|PM).*?)\r?\n.*(MEASURED VELOCITY:.*?$).*
下面是代码:

using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
{
    string mydirpath = "C:\\chat\\";

    string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

    Regex regex = new Regex("(^.*?(?:AM|PM).*?)\r?\n.*(MEASURED VELOCITY:.*?$).*",
        RegexOptions.Multiline | RegexOptions.Singleline);
    foreach (string txtName in txtFileList)
    {
        using (System.IO.StreamReader sr = new System.IO.StreamReader(txtName))
        {
            string text = sr.ReadToEnd();
            sw.WriteLine(regex.Replace(text, "$1, $2"));
        }
    }
}
给定文件的输出示例:

2014年2月20日上午7:33:10,实测流速:0.2225


下面是我的建议,让full Main()尽可能多地使用代码中的内容。在
语句之外声明vars,而
语句不需要将其设置为null

编辑-我忘了你说过:

每个读取文件都有几个这样的项

所以添加了几行来处理这个问题

static void Main(string[] args)
{
    string line;
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
        {
            string mydirpath = "C:\\chat\\";

            string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

            foreach (string txtName in txtFileList)
            {
                string spart = ".prt";
                string sam = " AM";
                string spm = " PM";
                string sresult = "TEST RESULT: ";
                string svelocity = "MEASURED VELOCITY: ";
                string part = string.Empty;
                string date = string.Empty;
                string result = string.Empty;
                string velocity = string.Empty;

                using (StreamReader sr = new StreamReader(txtName))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)
                        {
                            if (line.Contains(sam) || line.Contains(spm))
                            {
                                // Every new date means a new record. If you already have data for a record, first write it.
                                if (date != string.Empty && velocity != string.Empty)
                                {
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;
                                    sw.WriteLine(x[0] + "," + x[1]);
                                }
                                // Then reset data to prepare it for a new record
                                part = string.Empty;
                                result = string.Empty;
                                velocity = string.Empty;
                                date = line;
                            }

                            if (line.Contains(spart))
                            {
                                part = line;
                            }

                            if (line.Contains(sresult))
                            {
                                result = line;
                            }

                            if (line.Contains(svelocity))
                            {
                                velocity = line;
                            }
                        }
                    }
                }

                // After last record you still have some data to write
                if (date != string.Empty && velocity != string.Empty)
                {
                    int I = 2;
                    string[] x = new string[I];
                    x[0] = date;
                    x[1] = velocity;
                    sw.WriteLine(x[0] + "," + x[1]);
                }
            }
        }
    }
    catch
    {

    }
}

问题是,每次通过循环时都要写一行,例如,原始文件中的每行都要写一行。注意:已编辑-重置日期和速度应发生在if语句中。要重置if语句中的日期和速度,请单击“确定”,但是还需要在while语句之外初始化这两个变量。他还应该使用
using
语句或调用
dispose()
Sweet来处理
StreamReader
!我没有4.0 VS框架能够使用IsNullOrWhiteSpace方法,@TheodoreCross用自.NET 2.0以来有效的等效代码替换IsNullOrWhiteSpace
static void Main(string[] args)
{
    string line;
    try
    {
        using (StreamWriter sw = new StreamWriter("C:\\writetest\\writetest.txt"))
        {
            string mydirpath = "C:\\chat\\";

            string[] txtFileList = Directory.GetFiles(mydirpath, "*.txt");

            foreach (string txtName in txtFileList)
            {
                string spart = ".prt";
                string sam = " AM";
                string spm = " PM";
                string sresult = "TEST RESULT: ";
                string svelocity = "MEASURED VELOCITY: ";
                string part = string.Empty;
                string date = string.Empty;
                string result = string.Empty;
                string velocity = string.Empty;

                using (StreamReader sr = new StreamReader(txtName))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (!string.IsNullOrEmpty(line) && line.Trim().Length != 0)
                        {
                            if (line.Contains(sam) || line.Contains(spm))
                            {
                                // Every new date means a new record. If you already have data for a record, first write it.
                                if (date != string.Empty && velocity != string.Empty)
                                {
                                    int I = 2;
                                    string[] x = new string[I];
                                    x[0] = date;
                                    x[1] = velocity;
                                    sw.WriteLine(x[0] + "," + x[1]);
                                }
                                // Then reset data to prepare it for a new record
                                part = string.Empty;
                                result = string.Empty;
                                velocity = string.Empty;
                                date = line;
                            }

                            if (line.Contains(spart))
                            {
                                part = line;
                            }

                            if (line.Contains(sresult))
                            {
                                result = line;
                            }

                            if (line.Contains(svelocity))
                            {
                                velocity = line;
                            }
                        }
                    }
                }

                // After last record you still have some data to write
                if (date != string.Empty && velocity != string.Empty)
                {
                    int I = 2;
                    string[] x = new string[I];
                    x[0] = date;
                    x[1] = velocity;
                    sw.WriteLine(x[0] + "," + x[1]);
                }
            }
        }
    }
    catch
    {

    }
}