C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,172 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,575 people online right now. Registration is fast and FREE... Join Now!




Serialisation and the FileSystemWatcher

 

Serialisation and the FileSystemWatcher, Marking a FileSystemWatcher object as serialisable

Methical

11 May, 2009 - 05:36 PM
Post #1

New D.I.C Head
*

Joined: 8 May, 2009
Posts: 8

Hey guys.

I need aid in learning how to serialise a FileSystemWatcher object.

I have a class Watch.cs:

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

[Serializable]
public class Watch
{
    FileSystemWatcher monitor;

    public Watch(string path)
   {
        monitor = new FileSystemWatcher(path);
   }
}



and I am serialising it like so;

CODE

FileStream saveStream = new FileStream(System.Environment.CurrentDirectory + "\\SavedObjects.fsd", FileMode.Create);
            
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(saveStream, watch);
saveStream.Close();


Now i am serialising 3 other classes of mine in the exact same way and it works fine. The difference being is that my Watch.cs class holds two instances of the FileSystemWatcher class. Whenever i try and serialise this Watch.cs class i get a SerialisationException saying "Type 'System.IO.FileSystemWatcher' in Assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."

Now I have done some brief searches on google but I cannot seem to find anyone else having a similar issue. I realise that to serialise something you must either mark it with [Serializable] attribute of make the class implement the ISerializable interface but neither is possible when its a variable within a class that's causing the fuss.

Do i really have to create my own wrapper class around this FileSystemWatcher just so i cant get it to implement the ISerlializable interface ? I cannot see that working either really as i would still have to have an instance of FileSystemWatcher within that class.

Is it possible to get a class containing a FileSystemWatcher to serialise through a BinaryFormatter or must i use the XML way of doing things because i am not so keen on that approach.

Thanks in advance for any help you may be able to give.

User is offlineProfile CardPM
+Quote Post


Posts in this topic

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:51PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month