Trying serialize when I close the window. I get this runtime error:
An unhandled exception of type
System.NotSupportedException
occurred in PresentationFramework.dllAdditional information: The given path's format is not supported.
This happens in two separate instances:
- Closing the window by using the exit button (top-right corner),
- 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();
}
}