从文件中读取部分内容的正确方法是什么?

时间:2015-10-21 09:43:51

标签: c#

我想从一个文本文件中读取部分内容,该文件包含以下内容和换行符:

Content-Type: multipart/signed;
    boundary="boundarytext"

--<boundarytext>
Content-Type: text/plain

{1:A}{2:B      N}{4:
:10:C123
:11:124
:43E:
test.txt
2010-03-20/09:37:45
Accepted
-}
--<boundarytext>

我期待以下内容:

{1:A}{2:B      N}{4:
:10:C123
:11:124
:43E:
test.txt
2010-03-20/09:37:45
Accepted
-}

目前正在尝试逐行读取文件。

有没有最好的方法来达到我的预期效果?

这是我正在使用的代码,

    using (var fileRead = new StreamReader(@"c:\temp\testfile.txt"))
    {
        var blockIdentifier = "{1:";
        var textBlockIdentifier = "-}";
        var fileContent = fileRead.ReadToEnd();
        var startPos = fileContent.LastIndexOf(blockIdentifier);
        var length = (fileContent.IndexOf(textBlockIdentifier) + 2) - startPos;
        var newContent = fileContent.Substring(startPos, length);
    }

由于

2 个答案:

答案 0 :(得分:2)

您可以使用LINQ:

string[] relevantLines = File.ReadLines(path)
    .SkipWhile(l => !l.StartsWith("--<boundarytext>"))
    .Skip(3)
    .TakeWhile(l => !l.StartsWith("--<boundarytext>"))
    .ToArray();

boundarytext似乎总是在变化,所以你需要先检测它:

string boundaryTextLine = File.ReadLines(path)
  .FirstOrDefault(l => l.IndexOf("boundary=", StringComparison.InvariantCultureIgnoreCase) >= 0);
if(boundaryTextLine != null)
{
    string boundaryText = boundaryTextLine
        .Substring(boundaryTextLine.IndexOf("boundary=", StringComparison.InvariantCultureIgnoreCase) + "boundary=".Length)
        .Trim(' ', '"');
}

答案 1 :(得分:0)

我个人会使用正则表达式

/(?<=\-\-<boundarytext>)\n(.+)\n(?=\-\-<boundarytext>)/gsU

请参阅:https://regex101.com/r/rB1tC8/1

我甚至会从邮件中解析content_type。为了您的传播,这是c#

的工作测试用例
        [TestMethod]
        public void TestMethod1()
        {
            var input = @"Content-Type: multipart/signed;
    boundary='boundarytext1'

--<boundarytext1>
Content-Type: text/plain

{1:A}{2:B      N}{4:
:10:C123
:11:124
:43E:
test.txt
2010-03-20/09:37:45
Accepted
-}
--<boundarytext1>

Content-Type: multipart/signed;
    boundary='boundarytext2'

--<boundarytext2>
Content-Type: text/plain

{1:A}{2:B      N}{4:
:10:C123
:11:124
:43E:
test.txt
2010-03-20/09:37:45
Accepted
-}
--<boundarytext2>".Replace("'", "\"");

            var pattern = @"

boundary='(.+)'         # capture the boundary delimiter in \1
.+
(--<\1>)                # every message starts with --<boundary>
.+
Content-Type:\s
(?<content_type>[\w/]+) # capture the content_type
(\r?\n)+
(?<content>.+?)         # capture the content
(\r?\n)+
(--<\1>)                # every message ends with --<boundary>

".Replace("'", "\"");
            var regex = new Regex(pattern, 
                RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
            var matches = regex.Matches(input);
            Assert.AreEqual(2, matches.Count);
            foreach (Match match in matches)
            {
                var content_type = match.Groups["content_type"].Value;
                var content = match.Groups["content"].Value;
                Assert.AreEqual("text/plain", content_type);
                Assert.IsTrue(content.StartsWith("{1") && content.EndsWith("-}"));
            }
        }
    }
相关问题