StreamReader数据输入到字典

时间:2018-12-11 09:39:45

标签: c# streamreader

我想使用c#将输出(从streamreader并逐行读取)中的值存储到字典中。输出看起来像下面提到的屏幕截图,其中它在每个文件的基础上执行。所示输出仅为1个文件,并且有多个类似的输出文件。

我想分配文件路径[例如c:\ testing \ sp1aexpress_ru.exe]作为键,并将其余的值添加到字典中。您能建议我如何完成这项任务吗?

到目前为止,我已经编写了以下代码。尽管有Console.WriteLine(line),但我想将其输入到字典中。

Process process = new Process();
process.StartInfo.FileName = Pathdir + "\\test.exe";
process.StartInfo.Arguments = "-a -h -s " + dir;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();

using (StreamReader streamReader = process.StandardOutput)
{
    while (!streamReader.EndOfStream)
    {
        string line = streamReader.ReadLine();
        Console.WriteLine(line);
    }
}

输出:

c:\ testing \ sp1aexpress_ru.exe:

已验证:已签名

签名日期:2003年1月7日11:29

签名日期:2003年1月7日11:29

证书状态:已签名

有效用法:代码签名

证书颁发者:Microsoft代码签名PCA

序列号:61 07 11 43 00 00 00 00 00 34

缩略图:282D9806C3DF7345929F64F5895EF2EA4AC29302

c:\ testing \ WindowsRightsManagementServicesSP2-KB979099-Client-amd64-ENU.exe:

已验证:已签名

签名日期:2010年1月14日下午1:35

签名日期:2010年1月14日下午1:35

签名人:

Microsoft Corporation

证书状态:已签名

有效用法:代码签名

证书颁发者:Microsoft代码签名PCA

序列号:61 01 CF 3E 00 00 00 00 00 0F

缩略图:9617094A1CFB59AE7C1F7DFDB6739E4E7C40508F

1 个答案:

答案 0 :(得分:0)

这非常棘手,可能第一次无法使用。我解析文本文件已有40多年了,始终必须对复杂的格式进行一些细微的调整,尤其是当您有多个诸如此类代码的签名者时。尝试代码,让我知道是否有任何问题。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace ConsoleApplication91
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.txt";
        static void Main(string[] args)
        {
            Certificate.Add(FILENAME);
        }
    }
    public enum STATE
    {
        HEADER_FIRST_ROW,
        HEADER_NOT_FIRST_ROW,
        SIGNER_NAME,
        SIGNER_DATA
    }
    public class Certificate
    {
        public static Dictionary<string, Certificate> certificateDictionary = new Dictionary<string, Certificate>();

        public string filename { get;set;}
        public string verified { get; set; }
        public DateTime signingDate1 { get; set; }
        public DateTime signingDate2 { get; set; }
        public string catalog { get; set; }
        public List<Signer> signers { get; set; }

        public static void Add(string filename)
        {
            IFormatProvider provider = CultureInfo.InvariantCulture;
            StreamReader reader = new StreamReader(filename);
            string line = "";
            Certificate certificate = null;
            int dateCount = 0;
            Signer signer = null;
            string name = "";
            string value = "";
            STATE state = STATE.HEADER_FIRST_ROW;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    switch (state)
                    {
                        case STATE.HEADER_FIRST_ROW:
                            certificate = new Certificate();
                            certificate.filename = line;
                            certificateDictionary.Add(certificate.filename, certificate);
                            dateCount = 0;
                            state = STATE.HEADER_NOT_FIRST_ROW;
                            break;
                        case STATE.HEADER_NOT_FIRST_ROW:
                            name = line.Substring(0, line.IndexOf(":")).Trim();
                            value = line.Substring(line.IndexOf(":") + 1).Trim();

                            switch (name)
                            {
                                case "Verified":
                                    certificate.verified = value;
                                    break;
                                case "Signing date":
                                    if (dateCount == 0)
                                    {
                                        dateCount = 1;
                                        certificate.signingDate1 = DateTime.ParseExact(value, "h:mm tt M/dd/yyyy", provider);
                                    }
                                    else
                                    {
                                        certificate.signingDate2 = DateTime.ParseExact(value, "h:mm tt M/dd/yyyy", provider);
                                    }
                                    break;
                                case "Catalog":
                                    certificate.catalog = value;
                                    break;
                                case "Signers":
                                    state = STATE.SIGNER_NAME;
                                    break;
                            }
                            break;

                        case STATE.SIGNER_NAME:
                            if(certificate.signers == null) certificate.signers = new List<Signer>();
                            signer = new Signer();
                            certificate.signers.Add(signer);
                            signer.name = line;
                            state = STATE.SIGNER_DATA;
                            break;
                        case STATE.SIGNER_DATA:
                            name = line.Substring(0, line.IndexOf(":")).Trim();
                            value = line.Substring(line.IndexOf(":") + 1).Trim();

                            switch (name)
                            {
                                case "Cert Status":
                                    signer.certificateStatus = value;
                                    break;
                                case "Valid Usage":
                                    signer.validUsage = value;
                                    break;
                                case "Cert Issuer":
                                    signer.certIssuer = value;
                                    break;
                                case "Serial Number":
                                    signer.serialNumber = value;
                                    break;
                                case "Thumbprint":
                                    signer.thumbPrint  = value;
                                    break;
                                case "Valid from":
                                    signer.fromDate = DateTime.ParseExact(value, "h:mm tt M/dd/yyyy", provider);
                                    break;
                                case "Valid to":
                                    signer.toDate = DateTime.ParseExact(value, "h:mm tt M/dd/yyyy", provider);
                                    state = STATE.SIGNER_NAME;
                                    break;
                            }
                            break;
                    }

                }
            }

        }
        public class Signer
        {
            public string name { get; set; }
            public string certificateStatus { get; set; }
            public string validUsage { get; set; }
            public string certIssuer { get; set; }
            public string serialNumber { get; set; }
            public string thumbPrint { get; set; }
            public DateTime fromDate { get; set; }
            public DateTime toDate { get; set; }
        }

    }


}