C# 需要扫描目录中的文本文件并根据循环结果进行筛选
我有一个程序,可以扫描目录中的文本文件,循环每一行,并根据每一行中的前缀进行解析。该程序充当提取器,从前缀“Hxx”上的Base64字符串中提取tif图像。当程序从“Hxx”行获取图像时,它只是删除原始文件 我想做的是保留行“TXA”的过滤条件,但不是将行“Hxx”上的字符串转换为图像并删除文件,而是保留文件的全部内容。基本上,仅使用程序根据行“TXA”的条件解析和过滤文本文件 我知道在我的C# 需要扫描目录中的文本文件并根据循环结果进行筛选,c#,C#,我有一个程序,可以扫描目录中的文本文件,循环每一行,并根据每一行中的前缀进行解析。该程序充当提取器,从前缀“Hxx”上的Base64字符串中提取tif图像。当程序从“Hxx”行获取图像时,它只是删除原始文件 我想做的是保留行“TXA”的过滤条件,但不是将行“Hxx”上的字符串转换为图像并删除文件,而是保留文件的全部内容。基本上,仅使用程序根据行“TXA”的条件解析和过滤文本文件 我知道在我的foreach循环的case“TXA”中,我需要将整个文件保存到内存流中,以便在程序结束时重新写入文件。我
foreach
循环的case
“TXA”中,我需要将整个文件保存到内存流中,以便在程序结束时重新写入文件。我只是不知道现在该怎么办
非常感谢您的帮助
/// <summary>
/// This method will open, read and parse out the image file and save it to disk.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
static bool ParseHFPFile(string inputFileName, string outputFileName)
{
List<MemoryStream> tiffStreamList = new List<MemoryStream>();
// 1. grab file contents.
string fileContents = ProgramHelper.GetFileContents(inputFileName);
if (string.IsNullOrEmpty(fileContents))
{
return false; // errors already raised.
}
Log("[O] ", false);
// 2. split file contents into a string array.
string[] fileContentStringList = fileContents.Split('\r');
if (fileContentStringList.Length == 0)
{
Log(" ERROR: Unable to split file contents on [CR] character.");
return false;
}
// 3. loop through the file lines and parse each "section" based on it's prefix.
string mrn = string.Empty;
string dos = string.Empty;
string imgType = string.Empty;
foreach (string line in fileContentStringList)
{
if (string.IsNullOrEmpty(line))
{
continue;
}
string prefix = line.Substring(0, 3);
switch (prefix)
{
case "MSH":
break;
case "EVN":
break;
case "PID":
mrn = line.Split('|')[3].Split('^')[0];
break;
case "PV1":
dos = line.Split('|')[44];
if (!String.IsNullOrWhiteSpace(dos))
{
dos = dos.Substring(0, 8);
}
break;
case "TXA":
imgType = line.Split('|')[2].Split('^')[0];
if (imgType == "EDH02" || imgType == "EDORDH")
{
Log("[NP]");
return true;
}
break;
case "OBX":
break;
case "Hxx":
// 0 - Hxx
// 1 - page number
// 2 - image type
// 3 - base64 encoded image.
// 1. split the line sections apart based on the pipe character ("|").
string[] hxxSections = line.Split('|');
byte[] decodedImageBytes = Convert.FromBase64String(hxxSections[3].Replace(@"\.br\", ""));
// 2. create a memory stream to store the byte array.
var ms = new MemoryStream();
ms.Write(decodedImageBytes, 0, decodedImageBytes.Length);
// 3. add the memory stream to a memory stream array for later use in saving.
tiffStreamList.Add(ms);
break;
case "Z":
break;
}
}
Log("[P] ", false);
// 4. write the memory streams to a new file.
ImageCodecInfo icInfo = ImageCodecInfo.GetImageEncoders().Single(c => c.MimeType == "image/tiff");
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
var ep = new EncoderParameters(1);
// 5. create references to the EncoderValues we will use
var ep1 = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
var ep2 = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
var ep3 = new EncoderParameter(enc, (long)EncoderValue.Flush);
string newOutputFilePath = Path.GetDirectoryName(outputFileName) + @"\";
string newOutputFileName = newOutputFilePath + mrn + "_" + dos + ".dat";
bool success = false;
int suffix = 1;
while (!success)
{
if (File.Exists(newOutputFileName))
{
newOutputFileName = newOutputFilePath + mrn + "_" + dos + "_" + suffix + ".dat";
}
else
{
success = true;
}
suffix++;
}
Log(string.Format("[NewFile: {0}] ", Path.GetFileName(newOutputFileName)), false);
var strm = new FileStream(newOutputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
System.Drawing.Image pages = null;
int frame = 0;
int pageCount = tiffStreamList.Count;
Log("[WT:", false);
try
{
foreach (MemoryStream m in tiffStreamList)
{
if (frame == 0)
{
ep.Param[0] = ep1;
pages = Image.FromStream(m, false, false);
pages.Save(strm, icInfo, ep);
}
else
{
ep.Param[0] = ep2;
if (pages != null)
pages.SaveAdd(Image.FromStream(m, false, false), ep);
}
if (frame == pageCount - 1)
{
ep.Param[0] = ep3;
if (pages != null)
pages.SaveAdd(ep);
}
frame++;
Log(".", false);
//m.Close();
m.Dispose();
}
Log("]");
return true;
}
catch (Exception ex)
{
Log(" EXCEPTION: " + ex.Message + Environment.NewLine + ex.StackTrace);
return false;
}
finally
{
}
}
//
///此方法将打开、读取和解析图像文件并将其保存到磁盘。
///
///
///
静态bool ParseHFPFile(字符串输入文件名、字符串输出文件名)
{
List=new List();
//1.抓取文件内容。
字符串fileContents=ProgramHelper.GetFileContents(inputFileName);
if(string.IsNullOrEmpty(fileContents))
{
return false;//已引发错误。
}
日志(“[O]”,false);
//2.将文件内容拆分为字符串数组。
string[]fileContentStringList=fileContents.Split('\r');
如果(fileContentStringList.Length==0)
{
日志(“错误:无法拆分[CR]字符上的文件内容。”);
返回false;
}
//3.循环遍历文件行,并根据其前缀解析每个“节”。
string mrn=string.Empty;
string dos=string.Empty;
string imgType=string.Empty;
foreach(fileContentStringList中的字符串行)
{
if(string.IsNullOrEmpty(行))
{
继续;
}
字符串前缀=行。子字符串(0,3);
开关(前缀)
{
案例“MSH”:
打破
案例“EVN”:
打破
案例“PID”:
mrn=line.Split(“|”)[3]。Split(“^”)[0];
打破
案例“PV1”:
dos=行分割(“|”)[44];
如果(!String.IsNullOrWhiteSpace(dos))
{
dos=dos.子串(0,8);
}
打破
病例“TXA”:
imgType=line.Split('|')[2].Split('^')[0];
如果(imgType==“EDH02”| | imgType==“EDORDH”)
{
日志(“[NP]”);
返回true;
}
打破
案例“OBX”:
打破
案例“Hxx”:
//0-Hxx
//1-页码
//2-图像类型
//3-base64编码图像。
//1.根据管道字符(“|”)拆分线段。
字符串[]hxxSections=line.Split(“|”);
byte[]decodedImageBytes=Convert.FromBase64String(hxxSections[3]。替换(@“\.br\”,“”);
//2.创建存储字节数组的内存流。
var ms=新内存流();
ms.Write(decodedImageBytes,0,decodedImageBytes.Length);
//3.将内存流添加到内存流数组中,以便以后保存时使用。
添加(ms);
打破
案例“Z”:
打破
}
}
日志(“[P]”,假);
//4.将内存流写入新文件。
ImageCodecInfo icInfo=ImageCodecInfo.GetImageEncoders().Single(c=>c.MimeType==“image/tiff”);
System.Drawing.Imaging.enc=System.Drawing.Imaging.Encoder.SaveFlag;
var ep=新编码器参数(1);
//5.创建对我们将使用的encoderValue的引用
var ep1=新编码器参数(enc,(long)EncoderValue.MultiFrame);
var ep2=新编码器参数(enc,(long)EncoderValue.FrameDimensionPage);
var ep3=新编码器参数(enc,(long)EncoderValue.Flush);
字符串newOutputFilePath=Path.GetDirectoryName(outputFileName)+@“\”;
字符串newOutputFileName=newOutputFilePath+mrn+““+dos+”.dat”;
布尔成功=假;
int后缀=1;
而(!成功)
{
if(File.Exists(newOutputFileName))
{
newOutputFileName=newOutputFilePath+mrn+“\u”+dos+“\u”+后缀+“.dat”;
}
其他的
{
成功=真实;
}
后缀++;
}
日志(string.Format(“[NewFile:{0}]”,Path.GetFileName(newOutputFileName)),false);
var strm=newfilestream(newOutputFileName,FileMode.OpenOrCreate,FileAccess.ReadWrite);
System.Drawing.Image pages=空;
int帧=0;
int pageCount=tiffStreamList.Count;
日志(“[WT:,false”);
尝试
{
foreach(tiffStreamList中的MemoryStream m)
{
如果(帧==0)
{
ep.Param[0]=ep1;
pages=Image.FromStream(m,false,false);
保存页面(strm、icInfo、ep);
}
其他的
{
ep.Param[0]=ep2;
如果(页数!=null)
pages.SaveAdd(Image.FromStream(m,false,false),ep);
}
如果(帧)
List<string> TXA = new List<string>();
List<string> FilesForDelete = new List<string>();
if (fileIsTXA)
{
TXA.Add(fileName);
}
else
{
FilesForDelete.Add(fileName);
}