Hi
I am creating a method that reads a files contents. I thought about returning the StreamReader object but went against it as I cannot close the object prior to exiting the method if the objects data has yet to be received. So Im trying to populate an array with the contents of the file, but am now running into problems with initialisation as I do not know how many lines of text are in the file. I seem to find no info that tells me C# array initialisation can be changed, is this true? Can I initilise the array to a line count of the file? Can a single string with multiple lines be converted into an array? Am I going about this the wrong way?
Thanks
string[] astrFileContents = new string[20];
int intCount = 0;
StreamReader srdStreamRead;
srdStreamRead=File.OpenText(FilePath);
astrFileContents[intCount] = srdStreamRead.ReadLine();
while(astrFileContents[intCount] != null) {
intCount++;
astrFileContents[intCount] = srdStreamRead.ReadLine();
}
srdStreamRead.Close();
return astrFileContents;
I am creating a method that reads a files contents. I thought about returning the StreamReader object but went against it as I cannot close the object prior to exiting the method if the objects data has yet to be received. So Im trying to populate an array with the contents of the file, but am now running into problems with initialisation as I do not know how many lines of text are in the file. I seem to find no info that tells me C# array initialisation can be changed, is this true? Can I initilise the array to a line count of the file? Can a single string with multiple lines be converted into an array? Am I going about this the wrong way?
Thanks
string[] astrFileContents = new string[20];
int intCount = 0;
StreamReader srdStreamRead;
srdStreamRead=File.OpenText(FilePath);
astrFileContents[intCount] = srdStreamRead.ReadLine();
while(astrFileContents[intCount] != null) {
intCount++;
astrFileContents[intCount] = srdStreamRead.ReadLine();
}
srdStreamRead.Close();
return astrFileContents;