SD Card File Delete

I am sorry, but I continue to have problems converting my application from 4.1 to 4.3. The code I had that worked for deleting a file on the SD Card no longer works. I have been unsuccessful in finding a solution. The error I am getting is as follows:

#### Exception System.InvalidOperationException - 0x00000000 (1) ####
#### Message: The card is already mounted.
#### Gadgeteer.Modules.GHIElectronics.SDCard::Mount [IP: 000c] ####
#### SDCardTest.Program::StartControl [IP: 004c] ####
#### Gadgeteer.Modules.GHIElectronics.Joystick::OnJoystickEvent [IP: 0057] ####
#### System.Reflection.MethodBase::Invoke [IP: 0000] ####
#### Gadgeteer.Program::DoOperation [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 001d] ####

A first chance exception of type ‘System.InvalidOperationException’ occurred in GTM.GHIElectronics.SDCard.dll
Error invoking method “Gadgeteer.Modules.GHIElectronics.Joystick” (check arguments to Program.BeginInvoke are correct)

The read and write sections of the code works however the File.Delete does not. The code is as follows:

using System;
using System.Text;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace SDCardTest
{
    public partial class Program
    {

        public static string ParameterFilepath = @ "\SD\ParameterFile.txt";
        public string ResultsFilePath { get; set; }
        public string CurrentParameters = "1,1,0,4,0,0,1";
        public string[] StoredParameters = new string[7] { "1", "1", "0", "4", "0", "0", "1" };
        public static int[] WriteParameters = new int[7];
        bool SetParameters = false;

        
        
        void ProgramStarted()
        {

            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");

            joystick.Calibrate();
            joystick.JoystickPressed += new Joystick.JoystickEventHandler(joystick_JoystickPressed);  

            // SD Card

            //Mount the SD card
            //if (sdCard.IsCardMounted == false) sdCard.Mount(); Thread.Sleep(200);

             //Get the root directory
            ParameterFilepath = @ "\SD\ParameterFile.txt";

            GT.Timer timer = new GT.Timer(200);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            timer.Start();
        }

        void timer_Tick(GT.Timer timer)
        {
            timer.Stop();
        }

        public void joystick_JoystickPressed(Joystick sender, Joystick.ButtonState state)
        {
            Debug.Print("joystick pressed");
            StartControl();
        }

        public void StartControl()
        {
            if (sdCard.IsCardMounted == true)
            {
                if (File.Exists(ParameterFilepath) == true)
                {
                    Debug.Print("File Exists");
                    // Set current parameters from default or from stored parameters
                    ReadTidisParameter(sdCard, CurrentParameters, out SetParameters);

                    File.Delete(ParameterFilepath);

                    sdCard.Unmount();
                    sdCard.Mount();
                    //Thread.Sleep(200);
                }
                else
                {
                    Debug.Print("File Does Not Exist");
                }
            }

            Debug.Print("Done Reading");
            WriteTidisParameter();
            Debug.Print("Done Writing");
        }

        public void ReadTidisParameter(SDCard sdCard, string CurrentParameters, out bool SetParameters)
        {
            try
            {
                FileStream sdcardreader = new FileStream(ParameterFilepath, FileMode.Open, FileAccess.Read);
                byte[] data = new byte[100];
                int read_count = sdcardreader.Read(data, 0, data.Length);
                sdcardreader.Close();
                CurrentParameters = new string(Encoding.UTF8.GetChars(data), 0, read_count);
                Debug.Print(CurrentParameters);
            }
            catch
            {
                Debug.Print("FileStream Error");
            }

            SetParameters = true;
         }


        public void WriteTidisParameter()
        {
            if (sdCard.IsCardMounted == false) 
                { 
                    sdCard.Mount();
                    bool fs_ready = false;
                    RemovableMedia.Insert += (a, b) =>
                    {
                        fs_ready = true;
                    };
                    while (!fs_ready)
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                }
            string sdFilepath = ParameterFilepath;
            Debug.Print(sdFilepath);
            string rootDirectory = VolumeInfo.GetVolumes()[0].RootDirectory;
            sdFilepath = rootDirectory + @ "\ParameterFile.txt";
            Debug.Print(sdFilepath);
            FileStream sdcardstream = new FileStream(sdFilepath, FileMode.Create);
            string sdfileline = CurrentParameters;
            byte[] buffer = new byte[sdfileline.Length];
            buffer = Encoding.UTF8.GetBytes(sdfileline + "\r\n");
            sdcardstream.Write(buffer, 0, buffer.Length);
            //Thread.Sleep(100);
            sdcardstream.Close();
            sdCard.Unmount();
        }

    }
}

I am also having problems with opening serial ports but I am still researching the problem.

[quote]#### Exception System.InvalidOperationException - 0x00000000 (1) ####

Message: The card is already mounted.[/quote]

That seems pretty clear - somewhere your code is duplicating a mount. Looking at the stack, you have an "isCardMounted check that returns true and then do an Unmount() then a Mount(). I’d try adding a wait in that step… but can I ask why do you do that in the first place?

@ Brett - Thanks, that was some left over code that I didn’t notice, removed both the unmount and mount and it works. Though if I leave only the unmount I get a joystick argument error which is wierd. Anyway I am back to making progress on the conversion. I also found my serial port problem. My configure statement was set to stop bits = none. This apparently is not supported. I checked the tech specs on my serial devices and I don’t think that will a problem. However, I don’t know what to do if the device has stop bits of none.