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 月 27 日上午 11:29
签约日期:2003 年 1 月 27 日上午 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 代码签名 PCA
序列号:61 01 CF 3E 00 00 00 00 00 0F
指纹:9617094A1CFB59AE7C1F7DFDB6739E4E7C40508F
解决方案
这是非常棘手的,可能不会第一次工作。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; }
}
}
}
推荐阅读
- apache-drill - Drill 支持 FILTER WHERE 吗?
- python - 无法在 Django Production 中加载静态文件
- node.js - flex GAE 上的 NodeJS 中用于繁重转码任务的多线程
- javascript - 无法访问数组中的数据,name[0] 未定义
- shell - 如何在 Hadoop 中转义 ".\file_name" 以将其重命名为 "file_name" ,我尝试了一些类似的示例,但没有任何帮助
- c# - Unity UI“切换”关闭模式
- ldap - 在 Rocketchat 上注册 LDAP 身份验证
- android - 如果我在 Android Map SDK 中隐藏 Google 徽标而只显示 OpenStreetMap 瓷砖,我会违反 Google TOS 吗?
- python - 多个数据帧中存在一个 ID
- r - ggplot aes:alpha 得到“平滑”