1

For my latest WPF application, I've been using an System.Collections.Specialized.StringCollection to save and load strings. My current system works like a charm in Debug and Release, but when deployed to ClickOnce, the application crashes as soon as anything involving the settings are involved (loading or saving)! However, System.Collections.Specialized.StringCollections work just fine on their own if not a setting.

What could be causing these crashes?

Here are my systems:

    public static void SaveCharacter(string code)
        {
            // Declare a blank StringCollection
            StringCollection strings = new StringCollection();

            // Convert the current settings StringCollection into an array and combine with the blank one
            if (Settings.Default.SavedCharacters.Count > 0)
            {
                string[] tempArr= new string[Settings.Default.SavedCharacters.Count];
                Settings.Default.SavedCharacters.CopyTo(tempArr, 0);
                strings.AddRange(tempArr);
            }

            // Add the new string to the list
            strings.Add(code);

            // This new list is now saved as the setting itself
            Settings.Default.SavedCharacters = strings;
            Settings.Default.Save();
        }
        public static void RestoreCharacters()
        {
            foreach (string str in Settings.Default.SavedCharacters)
            {
                CreateCharacter(str, "l" + ID.ToString()); // This function works fine
                ID++; // So does this variable (it's a public static)
            }
        }

P.S. I tried a similar system with List<string> items, but no dice. Other settings involving strings, bools and ints work fine though.

P.P.S. Sidenote, does anyone know how to combine System.Collections.Specialized.StringCollections without having to convert one to an array?

David Makogon
  • 69,407
  • 21
  • 141
  • 189
ThomasFrew
  • 11
  • 3
  • 1
    What is the Exception when it crashes? Windows or the Runtime have to give you *something*. A likely culptript would be trust. ClickOnce is the kind of environment where only the highest level of trusted code can run. – Christopher Dec 07 '19 at 02:04
  • can you do try / catch to get the exception logged somewhere? – Jawad Dec 07 '19 at 02:04
  • There I two articles on proper Exception handling that I link often: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET | However I do not think you need extra code to get the Exception. The framework is awesome at making sure unhandeled Exceptions are logged somewhere. The Windows logs, when in doubt. But a explicit logging and exposing can still help. – Christopher Dec 07 '19 at 02:07
  • Thanks for all your help @Christopher and @Jawad! I managed to get the exception, and it goes as follows: ```System.NullReferenceException: Object reference not set to an instance of an object. at Taskit.CharacterWindowManagement.SaveCharacter(String code)```. Any suggestions? – ThomasFrew Dec 07 '19 at 02:35

1 Answers1

0

Answering my own question in case anyone else gets stuck with such an annoying bug! Before you can operate on a User Setting that has to be newed(), you need to create an instance of it to utilise instead. For example:

StringCollection foo = Settings.Default.SavedCharacters;

When it comes to saving such collections, simply using Settings.Default.SavedCharacters = foo; works fine!

ThomasFrew
  • 11
  • 3