So let's say I have this text file:
(*********************************************
Import-Export
Version := v24.00
Owner :=
Exported := Wed Oct 05 09:22:48 2016
Note: File encoded in UTF-8. Only edit file in a program
which supports UTF-8 (like Notepad, not Wordpad).
**********************************************)
IE_VER := 2.15;
CONTROLLER controllerName (ProcessorType := "1756-L71",
Major := 24,
TimeSlice := 20,
ShareUnusedTimeSlice := 1,
RedundancyEnabled := 0,
KeepTestEditsOnSwitchOver := 0,
DataTablePadPercentage := 50,
SecurityCode := 0,
ChangesToDetect := 16#ffff_ffff_ffff_ffff,
SFCExecutionControl := "CurrentActive",
SFCRestartPosition := "MostRecent",
SFCLastScan := "DontScan",
SerialNumber := 16#0000_0000,
MatchProjectToController := No,
CanUseRPIFromProducer := No,
InhibitAutomaticFirmwareUpdate := 0,
PassThroughConfiguration := EnabledWithAppend,
DownloadProjectDocumentationAndExtendedProperties := Yes)
MODULE Local (Parent := "Local",
ParentModPortId := 1,
CatalogNumber := "1756-L71",
Vendor := 1,
ProductType := 14,
ProductCode := 92,
Major := 24,
Minor := 11,
PortLabel := "RxBACKPLANE",
ChassisSize := 10,
Slot := 0,
Mode := 2#0000_0000_0000_0001,
CompatibleModule := 0,
KeyMask := 2#0000_0000_0001_1111)
END_MODULE
...
And the "..." marks the continuation of the text file. If I want to just read in everything up to and including that "END_MODULE" there into a string how would I do that?
My idea is: read the whole file into a string, parse it by newlines and creating a do while loop of concatenating those array elements into a single string until one of them contains "END_MODULE" but that seems sort of backwards? Because I'm splitting the string into an array and then concatenating it back into a single string again. Is there a faster way than my idea?
If you want read line by line, here is sample code:
var builder = new StringBuilder();
using (var file = File.OpenRead("your file")) {
using (var reader = new StreamReader(file)) {
string line;
while ((line = reader.ReadLine()) != "END_MODULE") {
builder.AppendLine(line);
}
}
}
string final = builder.ToString();
Benefit is that you don't read whole file into memory, only the part you need (which might help if you have really large file).