Main Site Documentation

Weird filestream error or bug or just me


#1

I have the following statement…



localdir="\\SD\\";
localFilename="log_35.csv.txt";

MethodA
 SDCard.createfile(localStream, localdir.ToString() + localFilename.ToString());

Method B
 SDCard.createfile(localStream,"\\SD\\log_35.csv.txt");

createfile(string filename)
{
 FileStream fileStream = new FileStream(filename, FileMode.Append);//get exception if MethodA!!!

}

Can anyone explain this?


#2

createfile does more then i’m showing, just want to convey the issue


#3

What kind of exception?


#4

Exception of type: System.IO.IOException


#5

There is an HRESULT code inside the exception object. It might give more details about the real cause

https://www.ghielectronics.com/community/codeshare/entry/218

In any case looks like a bug. I would report it on netmf codeplex.


#6

Thanks Architect.

Looks lie codeshare viewing code part is not working!


#7

I’be noticed that too. I should be able to download.


#8

this is what i get when i use your logging code

[quote] #### Exception System.InvalidCastException - CLR_E_INVALID_CAST (4) ####
#### Message:
#### BreakContinue.Diagnostics.HResults::HResultToString [IP: 0026] ####
#### zigbox.fezcobra2.SDCard::createfile [IP: 0090] ####
#### zigbox.fezcobra2.downloader::DownloadFile [IP: 00f0] ####
#### zigbox.fezcobra2.Program::scheduler_ActionResponse [IP: 0062] ####
#### mfnet.smallbiz.common.Scheduler::queue_ActionReady [IP: 0030] ####
#### mfnet.smallbiz.common.TimeQueue::ActionTime [IP: 0034] ####
A first chance exception of type ‘System.InvalidCastException’ occurred in zigbox.fezcobra2.exe
[/quote]


#9

Interesting it shows that there is an exception inside my HResultToString.
I have just downloaded the code from codeshare and tried it in emulator. HResultToString worked fine without the exception.

Can you show how do you call HResultToString?


#10

@ zigbox -

Interested issue :))

try this to see what is happened?


using System;
using System.Threading;
using Microsoft.SPOT;

namespace MFConsoleApplication1
{
    public class Program
    {
        static string text;
        public static void Main()
        {
            string localdir = "\\SD\\";
            string localFilename = "log_35.csv.txt";
            text = localdir.ToString() + localFilename.ToString();
            CreatFile("\\SD\\log_35.csv.txt");
            Thread.Sleep(-1);
        }
        static void CreatFile(string filename)
        {
            if (filename.CompareTo(text) == 0)
            {
                Debug.Print("They are same");
            }
            else
            {
                Debug.Print("Oh no not same");
            }

            FileStream fileStream = new FileStream(filename, FileMode.Append);
        }


    }
}



#11

tried the code but not luck. If it try to download the same file every minutes…do i have to check wther the files already exists or the filestream overwrite it by default?


#12

@ Architech calling your code liek this

 public static void createfile(Stream sdata, string filename)
        {
            if (!isMounted) MountSD();
            byte[] buffer = new byte[BUFFER_SIZE];
            sdata.Seek(0, SeekOrigin.Begin);
            long remainingBytes = sdata.Length;
            try
            {
                using (FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate,FileAccess.Write))
                {



                    // FileStream fileStream = new FileStream("\\SD\\test.txt", FileMode.Append);
                    while (remainingBytes > 0)
                    {
                        int nRead;
                        if (remainingBytes < BUFFER_SIZE)
                        {
                            nRead = sdata.Read(buffer, 0, (int)remainingBytes);
                        }
                        else
                        {
                            nRead = sdata.Read(buffer, 0, BUFFER_SIZE);
                        }

                        remainingBytes -= nRead;

                        fileStream.Write(buffer, 0, nRead);
                    }

                    if (fileStream != null) fileStream.Close();
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.HResultToString());

            }

        }