Simon Stochholm

Forum Replies Created

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • in reply to: Saving files #4442
    Author Image
    Simon Stochholm
    Participant

    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);
    }

    in reply to: Saving files #4440
    Author Image
    Simon Stochholm
    Participant

    [SOLVED]
    It turned out that I had forgotten to serialize part of my classes.
    In order to save files use this path:
    private static string filePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), “test.dat”);

    And then just read and write bytes to and from this filePath

    in reply to: Reading files #4437
    Author Image
    Simon Stochholm
    Participant

    Yes, I have seen that example. But if I try to use my own bitmaps it won’t work. It seems something special has been done to the file. For instance, it is actually a .bin file which has gotten a logical name of .bitmap, and the file cannot be opened by any paint program, so what has been done to the file? Can anyone give an example of reading different image files preferably bitmaps og rgf?

    in reply to: Saving files #4436
    Author Image
    Simon Stochholm
    Participant

    I need it for saving settings, so that I can read those settings if the ev3 has been turned off. I have tried the following ways:
    1) private static string path = Path.GetDirectoryName(Assembly.GetAssembly(typeof(AbstractMemory)).CodeBase);
    2) static string filePath = “/home/root/apps/test.dat”;
    3) static DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
    4)private static string filePath = “test.dat”;
    5)private static string filePath = Path.Combine(Environment.CurrentDirectory, “test.dat”);
    But none of them seems to work for me.

    Can anyone give a good example of saving to a file on the ev3, or just the correct way to find the correct path to save the file

Viewing 4 posts - 1 through 4 (of 4 total)
Posted in

Make a donation