开发者

XmlSerializer crashing only when "Start without debugging", but not "Step Into"

So as the title says, I'm having an issue with XmlSerializer. If I run "Step Into" it runs fine, and saves as it should. But if I "Start without debugging" it runs until the line

XmlSerializer serial = new XmlSerializer(typeof(ObservableCollection<Bill>));

at which point it crashes. I've tried putting a thread.sleep beforehand to make sure it's clear, but no help. I've been searching here and google all day...Please help me figure out the problem???

Here is the full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Win开发者_StackOverflow中文版dows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Data;
using System.Xml.Serialization;
using System.Collections.ObjectModel;


namespace BudgetHelper
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public Bills billsClass = new Bills();

    public MainWindow()
    {
        InitializeComponent();
        upcomingDueBills.ItemsSource = billsClass.PopulateUpcomingBills();
    }

    public void RefreshBills()
    {
        upcomingDueBills.Items.Refresh();
    }

    private void addBill_Click(object sender, RoutedEventArgs e)
    {
        AddAccount addAccountWindow = new AddAccount();
        addAccountWindow.ShowDialog();
        RefreshBills();
    }

    private void exitMenu_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        MessageBoxResult key = MessageBox.Show(
            "Are you sure you want to quit",
            "Confirm",
            MessageBoxButton.YesNo,
            MessageBoxImage.Question,
            MessageBoxResult.No);
        e.Cancel = (key == MessageBoxResult.No);
    }

    private void openMenu_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openDialog = new OpenFileDialog();
        openDialog.DefaultExt = ".txt";
        openDialog.FileName = "Text Documents (.txt)";

        Nullable<bool> result = openDialog.ShowDialog();

        if (result == true)
        {
            string filename = openDialog.FileName;
        }
    }

    private void updateBills_Click(object sender, RoutedEventArgs e)
    {
        upcomingDueBills.Items.Refresh();
    }



    private void saveMenu_Click(object sender, RoutedEventArgs e)
    {
        if (upcomingDueBills == null)
        {
            return;
        }
        else
        {
            billsClass.SaveBills();
        }
    }
}
}



using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using Microsoft.Win32;
using System.Threading;

namespace BudgetHelper
{
public class Bills : IEditableObject
{
    ObservableCollection<Bill> billCollection = new ObservableCollection<Bill>();

    public ObservableCollection<Bill> PopulateUpcomingBills()
    {


        using (StreamReader reader = new StreamReader(@"c:\users\Keven\Documents    
\Test.txt"))
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] fields = line.Split(new char[] { '|' });
                billCollection.Add(new Bill()
                {
                    accountName = fields[0],
                    projectedDueDate = DateTime.Parse(fields[1]),
                    projectedAmount = decimal.Parse(fields[2]),
                    totalDue = decimal.Parse(fields[3]),
                    category = fields[4],
                    notes = fields[5],
                    paidStatus = bool.Parse(fields[6])
                });


            }
        }

        return billCollection;
    }

    public void AddNewBill(string account, DateTime dueDate, decimal amount, decimal 
total, string category, string notes, bool isPaid)
    {
        billCollection.Add(new Bill()
        {
            accountName = account,
            projectedDueDate = dueDate,
            projectedAmount = amount,
            totalDue = total,
            category = category,
            notes = notes,
            paidStatus = isPaid
        });
    }

    public void SaveBills()
    {
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.AddExtension = true;
        saveDialog.DefaultExt = ".xml";
        saveDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
        saveDialog.InitialDirectory = @"C:\Users\Keven\Documents";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "New Account Details";
        saveDialog.ValidateNames = true;
        if (saveDialog.ShowDialog().Value)
        {
            Thread.Sleep(2000);
            MessageBox.Show("2");
            XmlSerializer serial = new XmlSerializer(typeof
(ObservableCollection<Bill>));
            using (StreamWriter writer = new StreamWriter(saveDialog.FileName))
            {

                serial.Serialize(writer, billCollection);

            }
        }
    }

    public static MainWindow main;

    public object UpcomingDueBills
    {
        get;
        set;
    }

    public class Bill
    {
        public string accountName { get; set; }
        public DateTime projectedDueDate { get; set; }
        public decimal projectedAmount { get; set; }
        public decimal totalDue { get; set; }
        public string category { get; set; }
        public string notes { get; set; }
        public bool paidStatus { get; set; }
    }

    void IEditableObject.BeginEdit()
    {
        throw new NotImplementedException();
    }

    void IEditableObject.CancelEdit()
    {
        throw new NotImplementedException();
    }

    void IEditableObject.EndEdit()
    {
        throw new NotImplementedException();
    }
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using Microsoft.Win32;
using System.Windows.Threading;
using System.Threading;
using System.Data;

namespace BudgetHelper
{
/// <summary>
/// Interaction logic for AddAccount.xaml
/// </summary>
public partial class AddAccount : Window
{
    public Bills bills = new Bills();

    public AddAccount()
    {
        InitializeComponent();
    }

    private void cancel_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void saveAccountInfo_Click(object sender, RoutedEventArgs e)
    {
        SaveFileDialog saveDialog = new SaveFileDialog();
        saveDialog.AddExtension = true;
        saveDialog.DefaultExt = "txt";
        saveDialog.InitialDirectory = @"C:\Users\Keven\Documents";
        saveDialog.OverwritePrompt = true;
        saveDialog.Title = "New Account Details";
        saveDialog.ValidateNames = true;
        if (saveDialog.ShowDialog().Value)
        {

        }

        this.Close();
    }

    private void done_Click(object sender, RoutedEventArgs e)
    {
        bills.AddNewBill(newAccountName.Text, projectedDueDate.SelectedDate.Value, 
decimal.Parse(projectedAmount.Text.ToString()),
            decimal.Parse(totalDue.Text.ToString()), accountCategory.Text, 
accountNotes.Text, (bool)isPaid.IsChecked);
    }

}
}


Found the answer, it was Comodo sandboxing the application automatically. If anyone else has this issue, just go to Defense+ in Comodo, open the Unrecognized Files window, and add the program to the trusted files list.

On another note, does anybody know if there's a way to make Comodo trust ALL Visual Studio projects that I work on?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜