代码之家  ›  专栏  ›  技术社区  ›  Maslow

WPF公司Properties.Settings 启动和关闭时引发异常

  •  0
  • Maslow  · 技术社区  · 15 年前

    当我的Wpf应用程序启动时,我在Settings.Designer.cs

    [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        public global::System.Collections.Specialized.StringCollection Projects {
            get {
                return ((global::System.Collections.Specialized.StringCollection)(this["Projects"]));
            }
            set {
                this["Projects"] = value;
            }
        }
    

    The assembly with display name 'System.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'System.XmlSerializers, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
    

    在我的主窗口代码后面,我正在执行以下操作 window_loaded :

    try
            {
              if (Properties.Settings.Default.Projects==null)
                Properties.Settings.Default.Projects=new StringCollection( );
            var removalList = new List<string>( );
            foreach (string project in Properties.Settings.Default.Projects)
            {
                if (System.IO.File.Exists(project))
                    OpenProject(project);
                else
                {
                    removalList.Add(project);
                }
    
            }
            foreach (string missingProject in removalList) //so that we are not removing an item while iterating
            {
                Properties.Settings.Default.Projects.Remove(missingProject);
            }
                }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString( ));
            }
    

    也在 window_closing

    try
                {
                    //TODO: save prompt
                    Properties.Settings.Default.Save( );
                }
                catch (Exception ex)
                {
    
                    Debug.WriteLine(ex.ToString( ));
                }
    

    这也引发了同样的异常。为什么我在访问 Properties.Settings ?

    1 回复  |  直到 15 年前
        1
  •  2
  •   Daniel Pratt    15 年前

    这种例外在我的经验中并不罕见。什么 不寻常的是,在您的情况下,异常(显然)没有得到处理。通常,(在我的经验中也是如此)这个异常会被其他机制默默地捕获和处理。

    你在VS调试器中运行这个程序时,不会出现“抛出异常时中断”的情况吧?该设置正在调试中->例外…,顺便说一下。

    推荐文章