Storing on usb memory stick

I am detecting the memory stick & mounting the file system…no problem

  Debug.Print("USB Mass Storage detected...");
               ps = new PersistentStorage(device);
                ps.MountFileSystem();

but when I try to add code to set up a file, it raises an exception:

     FileStream FileHandle = new FileStream( "hello.txt", FileMode.Create);    // exception happens here!!!
            byte[] data = 
               Encoding.UTF8.GetBytes("This string was brought to you by big bear");
            // write the data and close the file
            FileHandle.Write(data, 0, data.Length);
            FileHandle.Close();
            // if we need to unmount
            ps.UnmountFileSystem();

GHIElectronics.NETMF.USBHost.USBHostController::nativeEventDispatcher_OnInterrupt [IP: 0037] ####
#### GHIElectronics.NETMF.System.InternalEvent::nativeEventDispatcher_OnInterrupt [IP: 0054] ####
A first chance exception of type ‘System.IO.IOException’ occurred in System.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in System.IO.dll

Is this the proper setup for a usb stick (to create/open a file)?

Aad a delay between mounting the filesystem and the filestream

And besides that, you are missing the root directory declaration:


string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            FileStream FileHandle = new FileStream(rootDirectory +@ "\hello.txt", FileMode.Create);

Also, have a look here: GHI Electronics – Where Hardware Meets Software

I took out the root just to make it bare-bones for investigating the error…

I tried adding a 1second delay between mounting & filestream…no luck

Your usb stick is FAT formatted?

Your question is missing one or more of these details:[ul]
The name of the product.
The code you’re trying to run.
Details on your setup.[/ul]
(Generated by QuickReply)

Also provide a minimal full code example.

yes the stick is formatted…there are already some PC files on it.

The code was already provided…after mounting the storage, how can you create a new file on the flash USB stick?

please answer the questions…

To create a file on a monted filesystem, have a look on Filestream class.

what is wrong with this?

FileStream FileHandle = new FileStream( "hello.txt", FileMode.Create);  

does it at least appear viable?

“hello.txt” does not define a full path.

Here’s is what I am using but thi is not the only way to create a file:

FileStream fs = File.Create("\\SD\\" + file_name);

what is \SD\ & where is that described? Is this for a USB stick?

I’ll give it a try

“\SD\” is in my case the root directory of the µSD card. In your case, you will probably need “\USB\”

where is thes info described—I have not seen it before

check out this codeshare too, that talks about USB storage http://www.tinyclr.com/codeshare/entry/319

Please have a look on GHI Electronics – Where Hardware Meets Software

Then you can search on the .netmf library.

That link looks useful…but where is “\USB\” described?

There is no “\USB\” ! See below:

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;
 
namespace Test
{
    class Program
    {
 
        // Hold a static reference in case the GC kicks in and disposes it 
        // automatically, note that we only support one in this example!
        static PersistentStorage ps;
 
        public static void Main()
        {
            // Subscribe to RemovableMedia events
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;
 
            // Subscribe to USB events
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
 
            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }
 
        static void DeviceConnectedEvent(USBH_Device device)
        {
            if (device.TYPE == USBH_DeviceType.MassStorage)
            {
                Debug.Print("USB Mass Storage detected...");
                ps = new PersistentStorage(device);
                ps.MountFileSystem();
            }
        }
 
        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is inserted.");
            Debug.Print("Getting files and folders:");
            if (e.Volume.IsFormatted)
            {
                string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = 
                           Directory.GetDirectories(e.Volume.RootDirectory);
 
                Debug.Print("Files available on " + 
                            e.Volume.RootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);
 
                Debug.Print("Folders available on " + 
                            e.Volume.RootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with 
                            FAT32/FAT16 first.");
            }
        }
 
        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is ejected.");
        }
    }
}

@ Hoyt, maybe you should start small and simple. I have written \USB\ as a way to explicitely define the path to the root directory. Otherwise, as described by the Code written by Eric, you can use

e.Volume.RootDirectory

Actullay I believe that U should take the code written by Eric as it is. Run it and try to check if you succeed to use it. Then try to modify it and if U need explanations do not hesitate to ask us.

below is the ghi code–it works & give the directories…then I add some more GHI sample (see where says “ADDED”) to create & write a file…then it bombs out …why?
this is with pendrive usb memory stick

using System;
using System.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using System.Text;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.USBHost;


 
namespace Test
{
    class Program
    {
 
        // Hold a static reference in case the GC kicks in and disposes it 
        // automatically, note that we only support one in this example!
        static PersistentStorage ps;
 
        public static void Main()
        {
            // Subscribe to RemovableMedia events
            RemovableMedia.Insert += RemovableMedia_Insert;
            RemovableMedia.Eject += RemovableMedia_Eject;
 
            // Subscribe to USB events
            USBHostController.DeviceConnectedEvent += DeviceConnectedEvent;
 
            // Sleep forever
            Thread.Sleep(Timeout.Infinite);
        }
 
        static void DeviceConnectedEvent(USBH_Device device)
        {
            if (device.TYPE == USBH_DeviceType.MassStorage)
            {
                Debug.Print("USB Mass Storage detected...");
                ps = new PersistentStorage(device);
                ps.MountFileSystem();
                Thread.Sleep(1111);
                //========below is added==========

                // Assume one storage device is available, 
                // access it through NETMF
                string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
                FileStream FileHandle = new FileStream(rootDirectory + @ "\hello.txt", FileMode.Create);
                byte[] data =
                   Encoding.UTF8.GetBytes("This string will go in the file!");
                // write the data and close the file
                FileHandle.Write(data, 0, data.Length);
                FileHandle.Close();

                // if we need to unmount
                ps.UnmountFileSystem();
                //==================

            }
        }
 
        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is inserted.");
            Debug.Print("Getting files and folders:");
            if (e.Volume.IsFormatted)
            {
                string[] files = Directory.GetFiles(e.Volume.RootDirectory);
                string[] folders = 
                           Directory.GetDirectories(e.Volume.RootDirectory);
 
                Debug.Print("Files available on " + 
                            e.Volume.RootDirectory + ":");
                for (int i = 0; i < files.Length; i++)
                    Debug.Print(files[i]);
 
                Debug.Print("Folders available on " + 
                            e.Volume.RootDirectory + ":");
                for (int i = 0; i < folders.Length; i++)
                    Debug.Print(folders[i]);
            }
            else
            {
                Debug.Print("Storage is not formatted. Format on PC with FAT32/FAT16 first.");
            }
        }
 
        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Storage \"" + e.Volume.RootDirectory + 
                        "\" is ejected.");
        }
    }
}

USB Mass Storage detected…
#### Exception System.IO.IOException - CLR_E_FILE_IO (4) ####
#### Message:
#### Microsoft.SPOT.IO.VolumeInfo::.ctor [IP: 0000] ####
#### Microsoft.SPOT.IO.RemovableMedia::MessageHandler [IP: 0022] ####
A first chance exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type ‘System.IO.IOException’ occurred in Microsoft.SPOT.IO.dll

Why not wait until you get the RemovableMedia_Insert event before doing anything?

I can’t test right now, but I suspect that your code to create the file should be moved to the RemovableMedia.Insert event handler, my suspicion is that you can only access the storage device once the media is registered as inserted. That is pure conjecture of course, but maybe worth a shot.