none
Order of the settings RRS feed

  • Question

  • Hello everybody,
    I write a program to treat pictures that can be stored in a variable number of folders.
    The paths to the folders are stored in the properties settings, in user scope.
    So, when loading the form I launch this :

    		List<string> LFldrs = new List<string>();
    		SettingsPropertyCollection collection = Properties.Settings.Default.Properties;
    		foreach(SettingsProperty sp in collection)
    		{
    			if(sp.Name.StartsWith("Path"))
    			{
    				LFldrs.Add(sp.DefaultValue.ToString());
    			}
    		}
    
    Well, OK I have my folders in a List, but they are ordered Path2, Path1, Path4, Path3 (and the order of creation is Path1, Path2, Path3, Path4).

    OK I could put sp.Name in a SortedList, and then access the settings by name, but I feel I missed something, no ?



    • Edited by Gloops Sunday, June 9, 2019 9:09 PM
    Sunday, June 9, 2019 9:05 PM

Answers

  • Hello,

    How about sorting them as per below.

    var result = Properties.Settings.Default
        .Properties.OfType<SettingsProperty>().OrderBy(s => s.Name);
    
    List<string> LFldrs = new List<string>();           
    foreach (SettingsProperty sp in result)
    {
        if (sp.Name.StartsWith("Path"))
        {
            LFldrs.Add(sp.DefaultValue.ToString());
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Gloops Monday, June 10, 2019 12:26 AM
    Sunday, June 9, 2019 11:58 PM
    Moderator

All replies

  • Hello,

    How about sorting them as per below.

    var result = Properties.Settings.Default
        .Properties.OfType<SettingsProperty>().OrderBy(s => s.Name);
    
    List<string> LFldrs = new List<string>();           
    foreach (SettingsProperty sp in result)
    {
        if (sp.Name.StartsWith("Path"))
        {
            LFldrs.Add(sp.DefaultValue.ToString());
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Gloops Monday, June 10, 2019 12:26 AM
    Sunday, June 9, 2019 11:58 PM
    Moderator
  • Works nice, thank you.

    I fully typed the collection, but did not think I had to precise the type before asking the sort, so the intellisense did not propose me the OrderBy.

    Monday, June 10, 2019 12:25 AM