Skip to content

Instantly share code, notes, and snippets.

@gjulianm
Created February 26, 2012 16:05
Show Gist options
  • Save gjulianm/1917470 to your computer and use it in GitHub Desktop.
Save gjulianm/1917470 to your computer and use it in GitHub Desktop.
C# - IsolatedStorageFileStream Extension - WriteLines, ReadLines
// It will probably work in other streams.
public static class IsolatedFileStreamExtension
{
public static IEnumerable<string> ReadLines(this IsolatedStorageFileStream File)
{
char separator = char.MaxValue;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
List<string> Strings = new List<string>();
string contents;
byte[] bytes= new byte[File.Length];
string line = "";
int NewlineIndex;
File.Read(bytes, 0, (int)File.Length);
contents = new string(encoding.GetChars(bytes));
while ((NewlineIndex = contents.IndexOf(separator)) != -1)
{
line = contents.Substring(0, NewlineIndex);
contents = contents.Substring(NewlineIndex + 1);
yield return line;
}
}
public static string ReadLine(this IsolatedStorageFileStream File)
{
IEnumerable<string> Lines = File.ReadLines();
if(Lines != null && Lines.Count() != 0)
return Lines.First();
return "";
}
public static void WriteLines(this IsolatedStorageFileStream File, IEnumerable<string> Lines)
{
char separator = char.MaxValue;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
string contents = "";
byte[] bytes;
foreach (var str in Lines)
contents += str + separator;
bytes = encoding.GetBytes(contents);
File.Write(bytes, 0, bytes.Length);
}
public static void WriteLine(this IsolatedStorageFileStream File, string Line)
{
List<string> Lines = new List<string>();
Lines.Add(Line);
File.WriteLines(Lines);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment