Ich möchte einige Tipps und Hilfe zu einem Lesen/Schreiben Teil meiner C#.StreamWriter: Start und Ende auf einer bestimmten Zeilennummer
Situation:
- Ich habe eine CSV-Datei zu lesen; - OK
- Wenn der Name der CSV-Datei mit "Load_" beginnt, möchte ich auf eine andere CSV die Daten von Zeile 2 bis zum letzten schreiben;
- Wenn der Name der CSV-Datei mit "RO_" beginnt, möchte ich auf zwei verschiedene CSVs schreiben, 1 mit den Zeilen 1 bis 4 und die anderen 4 mit dem letzten;
Was ich bisher habe, ist:
public static void ProcessFile(string[] ProcessFile)
{
// Keeps track of your current position within a record
int wCurrLine = 0;
// Number of rows in the file that constitute a record
const int LINES_PER_ROW = 1;
int ctr = 0;
foreach (string filename in ProcessFile)
{
var sbText = new System.Text.StringBuilder(100000);
int stop_line = 0;
int start_line = 0;
// Used for the output name of the file
var dir = Path.GetDirectoryName(filename);
var fileName = Path.GetFileNameWithoutExtension(filename);
var ext = Path.GetExtension(filename);
var folderbefore = Path.GetFullPath(Path.Combine(dir, @"..\"));
var lineCount = File.ReadAllLines(@filename).Length;
string outputname = folderbefore + "output\\" + fileName;
using (StreamReader Reader = new StreamReader(@filename))
{
if (filename.Contains("RO_"))
{
start_line = 1;
stop_line = 5;
}
else
{
start_line = 2;
stop_line = lineCount;
}
ctr = 0;
while (!Reader.EndOfStream && ctr < stop_line)
{
// Add the text
sbText.Append(Reader.ReadLine());
// Increment our current record row counter
wCurrLine++;
// If we have read all of the rows for this record
if (wCurrLine == LINES_PER_ROW)
{
// Add a line to our buffer
sbText.AppendLine();
// And reset our record row count
wCurrLine = 0;
}
ctr++;
} // end of the while
}
int total_lenght = sbText.Length
// When all of the data has been loaded, write it to the text box in one fell swoop
using (StreamWriter Writer = new StreamWriter(dir + "\\" + "output\\" + fileName + "_out" + ext))
{
Writer.Write.(sbText.);
}
} // end of the foreach
} // end of ProcessFile
Ich dachte über die IF/ELSE mit: „Verwendung (Stream Writer = new Stream (dir + "\" + "output \" + Dateiname + "_out" + ext)) "Teil. Ich bin mir jedoch nicht sicher, wie ich an StreamWriter übergeben soll, um nur von/zu einer bestimmten Zeilennummer zu schreiben.
Jede Hilfe ist willkommen! Wenn ich ein paar Informationen verpasse, lass es mich wissen (ich bin ziemlich neu bei stackoverflow).
Vielen Dank.