Nguyen Le Bang Linh
Nguyen Le Bang Linh

Reputation: 23

Streamwriter throws a system.argumentexception

I know this question seems strange.

I use .NET Micro Framework to write a small program in C# that use the default emulator to emulate a flash light using 5 buttons on the emulator, using interruptport to raise events.

I coded so that when i pressed the bottom button, all the records stored in an arraylist usagelog will be printed out to a txt file. Very simple and straightforward, i made a Streamwriter instance

StreamWriter sw = new StreamWriter(@"c:\temp.txt");

But then it throws "An unhandled exception of type 'System.ArgumentException' occurred in System.IO.dll" at this line.

I can't fix this, and I can't understand why there's an argument exception here. The code works fine for a console project in visual C#, but it doesn't in Micro Framework.

Upvotes: 2

Views: 1318

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

The problem you are having is the because the FileSystem is different between Windows and the MicroFramework. I was able to get it to run on the Emulator by using some Directory Functions to determine the available directorys.

public static void Main()
{
    string  d = Directory.GetCurrentDirectory();
    string[] directorys = Directory.GetDirectories(d);
    foreach (var item in directorys )
    {
        Debug.Print(item);
    }

    try
    {
        using (StreamWriter sw = new StreamWriter("\\WINFS\\temp.txt"))
        {
            sw.WriteLine("Good Evening");
            sw.Close();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

}

In the Emulator I came up with

 [0]: "\\ROOT"
 [1]: "\\WINFS"

ROOT did not work but WINFS did.

Upvotes: 1

Related Questions