Finding Files on an SD Memory Card (English)

By Marco Minerva.  Translated by Mike Dodaro from the original Italian version.

.NET Gadgeteer makes useful APIs available to interact with an SD memory card in a module such as this SD Card Module available from GHI Electronics. It is possible to get a list of files, directories, to create files, and to read and write to them. Missing is a method that returns only files with a certain extension; we therefore see an option to add to the functionality by creating an extension method for the StorageDevice class. The following code assumes that the ExtensionAttribute class is referenced in the project; this class is introduced in the post Extension Methods with .NET Micro Framework.

using System;
using System.Collections;
using Microsoft.SPOT;

using GT = Gadgeteer;

namespace SearchFilesExamples
{
    public static class StorageExtensions
    {
        public static string[] GetFiles(this GT.StorageDevice storage, string extension)
        {
        return GetFiles(storage, null, extension, false);
        }

        public static string[] GetFiles(this GT.StorageDevice storage, string extension,
        bool recursive)
        {
            return GetFiles(storage, null, extension, recursive);
        }

        public static string[] GetFiles(this GT.StorageDevice storage, string path,
                                                                       string extension)
        {
            return GetFiles(storage, path, extension, false);
        }

        public static string[] GetFiles(this GT.StorageDevice storage, string path,
        string extension, bool recursive)
        {
            if (extension[0] != '.')
                extension = '.' + extension;

            ArrayList list = new ArrayList();
            GetFilesHelper(storage, path, extension, recursive, list);
            return (string[])list.ToArray(typeof(string));
        }

        private static void GetFilesHelper(GT.StorageDevice storage, string path,
        string extension, bool recursive, ArrayList list)
        {
            var files = storage.ListFiles(path);
            foreach (var fileName in files)
            {
                if (System.IO.Path.GetExtension(fileName).ToLower() == extension.ToLower())
                    list.Add(fileName);
            }

            if (recursive)
            {
                var directories = storage.ListDirectories(path);
                foreach (var directoryName in directories)
                GetFilesHelper(storage, directoryName, extension, recursive, list);
            }
        }
    }
}

Several available overloads specify, beyond the obvious file extension, the folder in which to search and whether the search must be recursive.

We use these new methods in an application that uses the GHI Electronics Display T35 and Button Module.  When the button is pressed, the code searches all files on the card with the TXT extension.

private Text txtMessage;
private Text txtResults;

private void SetupWindow()
{
    Window window = display.WPFWindow;
    Font baseFont = Resources.GetFont(Resources.FontResources.NinaB);

    Canvas canvas = new Canvas();
    window.Child = canvas;

    Text txtIntro = new Text(baseFont, "Press the button to search for TXT files.");
    canvas.Children.Add(txtIntro);
    Canvas.SetTop(txtIntro, 30);
    Canvas.SetLeft(txtIntro, 20);

    txtResults = new Text(baseFont, string.Empty);
    canvas.Children.Add(txtResults);
    Canvas.SetTop(txtResults, 60);
    Canvas.SetLeft(txtResults, 35);

    txtMessage = new Text(baseFont, string.Empty);
    txtMessage.Width = 300;
    txtMessage.TextWrap = true;
    canvas.Children.Add(txtMessage);
    Canvas.SetTop(txtMessage, 110);
    Canvas.SetLeft(txtMessage, 10);
}

private void button_ButtonPressed(Button sender, Button.ButtonState state)
{
    if (sdCard.IsCardMounted)
    {
        GT.StorageDevice storage = sdCard.GetStorageDevice();
        var textFiles = storage.GetFiles("txt", true);

        txtResults.TextContent = "Your SD Card contains " + textFiles.Length + " TXT files";

        string fileList = string.Empty;
        foreach (var fileName in textFiles)
        fileList += fileName + ", ";
        if (fileList.Length > 0)
        fileList = fileList.Substring(0, fileList.Length - 2);

        txtMessage.TextContent = fileList;
    }
}

In the ButtonPressed event handler, we verify that the memory card is available (It’s done with the Boolean property: IsCardMounted). We retrieve the object for management through the GetStorageDevice method.  At this point, we can use the new extension method to search for files with extension TXT. Not having indicated a path, the search starts from the root of the card; the parameter true indicates that we want to carry out a recursive search.  Finally, we show the total number and the complete names of the files found.

The complete Program.cs file of our application follows:

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Collections;

namespace SearchFilesExamples
{
    public partial class Program
    {
        void ProgramStarted()
        {
            SetupWindow();
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);

            Debug.Print("Program Started");
        }

        private Text txtMessage;
        private Text txtResults;

        private void SetupWindow()
        {
            Window window = display.WPFWindow;
            Font baseFont = Resources.GetFont(Resources.FontResources.NinaB);

            Canvas canvas = new Canvas();
            window.Child = canvas;

            Text txtIntro = new Text(baseFont, "Press the button to search for TXT files.");
            canvas.Children.Add(txtIntro);
            Canvas.SetTop(txtIntro, 30);
            Canvas.SetLeft(txtIntro, 20);

            txtResults = new Text(baseFont, string.Empty);
            canvas.Children.Add(txtResults);
            Canvas.SetTop(txtResults, 60);
            Canvas.SetLeft(txtResults, 35);

            txtMessage = new Text(baseFont, string.Empty);
            txtMessage.Width = 300;
            txtMessage.TextWrap = true;
            canvas.Children.Add(txtMessage);
            Canvas.SetTop(txtMessage, 110);
            Canvas.SetLeft(txtMessage, 10);
        }

        private void button_ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (sdCard.IsCardMounted)
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();
                var textFiles = storage.GetFiles("txt", true);

                txtResults.TextContent = "Your SD Card contains " + textFiles.Length + " TXT files";

                string fileList = string.Empty;
                foreach (var fileName in textFiles)
                    fileList += fileName + ", ";
                if (fileList.Length > 0)
                    fileList = fileList.Substring(0, fileList.Length - 2);

                txtMessage.TextContent = fileList;
            }
        }
    }
}

The example application is available for download.

SearchFilesExample.zip

Advertisements

,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: