Home→Forums→MonoBrick EV3 Firmware→Saving files→Reply To: Saving files
Simon Stochholm
What I have done is simply to serialize and deserialize an object, and then just return whatever object I need: (I had to do it this way, because the file was generated on the fly)
private static string filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), “test.dat”);
public static void Serialize(IDictionary<string, Action> combos)
{
var formatter = new BinaryFormatter();
var stream = new MemoryStream();
formatter.Serialize(stream, combos);
stream.Position = 0;
var bytes = stream.GetBuffer();
using (var streamer = File.Create(filePath))
{
streamer.Write(bytes, 0, bytes.Length);
}
}
public static IDictionary<string, Action> Deserialize()
{
var bytes = File.ReadAllBytes(filePath);
var formatter = new BinaryFormatter();
var stream = new MemoryStream(bytes);
var action = (IDictionary<string, Action>)formatter.Deserialize(stream);
return action;
}
If the file already exists on the EV3 you can simply do like this:
string text = System.IO.File.ReadAllText(filePath);
// Display the file contents to the console. Variable text is a string.
LcdConsole.WriteLine(“Contents of WriteText.txt = {0}”, text);
// Example #2
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(filePath);
// Display the file contents by using a foreach loop.
LcdConsole.WriteLine(“Contents of WriteLines2.txt = “);
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
LcdConsole.WriteLine(“\t” + line);
}
Follow