0

Trying serialize when I close the window. I get this runtime error:

An unhandled exception of type System.NotSupportedException occurred in PresentationFramework.dll

Additional information: The given path's format is not supported.

This happens in two separate instances:

  1. Closing the window by using the exit button (top-right corner),
  2. closing the window by pressing the Esc key.

On each respective instance one. No source code available, two this.Close() was highlighted when the error was raised (see KeysDown method in Window.xaml.cs).

Code (curtailed to fit this query):

Window.xaml.cs:

public partial class MainWindow : Window
{
    public SessionManager SM { get; private set; }

    public MainWindow()
    {
        InitializeComponent();
        //code
    }

    /// <summary>
    /// Registered to MainWindow.KeysDown Event
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="kea"></param>
    private void KeysDown(object sender, KeyEventArgs kea)
    {
        switch (kea.Key)
        {
            case Key.Escape:
                this.Close();
                break;
        }
    }

    private void SaveSession(object sender, CancelEventArgs e)
    {
        SM.SaveSession();
    }
}

SessionManager.cs (I had problems with the class variables, so I did not omit them):

[Serializable]
    public class SessionManager : DependencyObject
    {
        [NonSerialized]
        public static readonly DependencyProperty currentSessionProperty =
            DependencyProperty.Register("currentSession", typeof(Session),
            typeof(MainWindow), new FrameworkPropertyMetadata(null));
        [NonSerialized]
        public static readonly DependencyProperty startTimeProperty =
            DependencyProperty.Register("strStartTime", typeof(string),
            typeof(MainWindow), new FrameworkPropertyMetadata(DateTime.Now.ToString()));

        private static string SM_FILE = "SessionManager.bin";

        /// <summary>
        /// Holds a list of all comments made within the session
        /// </summary>
        public List<string> Sessions { get; private set; }
        [NonSerialized]
        private DateTime _dtStartTime;

        public SessionManager()
        {
            //code
        }


        #region Methods

        public void SaveSession()
        {
            if (currentSession.timeEntries.Count != 0)
            {
                currentSession.Save();
            }
            else
            {
                Sessions.Remove(currentSession.Name);
            }

            //Serialize the SessionManager
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(SM_FILE, FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream,this);
            stream.Close();
        }

        public void OpenSession(int index)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(Sessions[index], FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
            currentSession=(Session)formatter.Deserialize(stream);
            stream.Close();
        }
        #endregion
    }

Session.cs (I had problems with the class variables, so I did not omit them):

[Serializable]
public class Session : DependencyObject
{
    [NonSerialized]
    public static readonly DependencyProperty nameProperty =
        DependencyProperty.Register("name", typeof(string),
        typeof(Session), new FrameworkPropertyMetadata(string.Empty));

    [NonSerialized]
    public static readonly DependencyProperty totalTimeProperty =
        DependencyProperty.Register("totalTime", typeof(TimeSpan), typeof(Session),
        new PropertyMetadata(TimeSpan.Zero));

    public Session()
    {
        //code
    }

    public void Save()
    {
        IFormatter formatter = new BinaryFormatter();
        Stream stream= new FileStream(Name,FileMode.Create,FileAccess.Write,FileShare.None);
        formatter.Serialize(stream, this);
        stream.Close();
    }
}
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Skello
  • 333
  • 2
  • 15
  • Do you have any events registered in your code with '+='? – jdweng Dec 14 '15 at 13:19
  • I thought that class that derives from DependencyObject cannot be serialized. http://stackoverflow.com/questions/7294650/can-a-class-derived-from-dependencyobject-marked-as-serializable – Valentin Dec 14 '15 at 13:22
  • Event Closing should be what you need. https://msdn.microsoft.com/en-us/library/system.windows.window.closing(v=vs.110).aspx – Slasko Dec 14 '15 at 13:22
  • Besides any other problems, registering the dependency properties in class SessionManager with MainWindow as owner type is wrong. It should read `DependencyProperty.Register("currentSession", typeof(Session), typeof(SessionManager), ...);` – Clemens Dec 14 '15 at 13:23
  • 1
    _"The given path's format is not supported."_ -- this would imply some kind of problem with the file path that you are trying to write to in the `Save` method: possibly invalid filename characters, or a permissions issue. – Steven Rands Dec 14 '15 at 13:27
  • @Clemens: oops didn't notice that. A while ago, I decided to move some stuff around to make it more organized, and I guess that was an artifact of the old software architecture. Will fix ASAP. – Skello Dec 14 '15 at 13:59
  • @StevenRands: I removed a touch too much code from **Session.cs**. In the constructor the Name variable is assigned a value that will later be the name of the .bin file: Name = DateTime.Now.ToString() + ".bin"; – Skello Dec 14 '15 at 14:01
  • 1
    I suspect that is your problem then. `DateTime.Now.ToString()` will probably return a string with a `:` in it, which is an invalid character for a filename. – Steven Rands Dec 14 '15 at 14:33
  • @StevenRands: Yes, I fixed that now. I'm back to the DependencyOjbect issue. Thanks for the help. – Skello Dec 14 '15 at 19:49

0 Answers0