views:

123

answers:

3

Hello,

I am in the process of designing an application that will allow you to find pictures (screen shots) made from certain programs. I will provide the locations of a few of the program in the application itself to get the user started.

I was wondering how I should go about adding new locations as the time goes on, my first thought was simply hard coding it into the application but this will mean the user has to reinstall it to make the changes take affect.

My second idea was to use an XML file to contain all the locations as well as other data, such as the name of the application. This also means the user can add their own locations if they wish as well as sharing them over the internet.

The second option seemed the best approach but then I had to think how would it be managed on the users computer. Ideally I'd like just a single .exe without the reliance on any external files such as the XML but this would bring me back to point one.

Would it be best to simply use the ClickOnce deployment to create an entry in the start menu and create a folder containing the .exe and the file names?

Thanks for the feedback, I don't want to start implementing the application until the design is nailed.

+3  A: 

Generally I would not hard-code anything into the application. That makes for an inflexible design as you rightly point out.

I usually store this type of configuration information in an XML file under the Local Application Data folder.

If your users are always (or frequently) internet connected, you could consider an alternative (or additional) ability to store this kind of "bookmark" information on a web service.

EDIT:

Here's a code snippet I have used over the years for this sort of thing

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace JTools.IO.Configuration
{
    public static class ConfigManager<T> where T : new()
    {
        private const string FILENAME = "Config.xml";

        public static T Load()
        {
            return Load(ConfigFile);
        }

        public static T Load(string fileName)
        {
            T ret = default(T);

            try
            {
                // Open file to read the data from

                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {

                    // Create an XmlSerializer object to perform the deserialization
                    XmlSerializer xs = new XmlSerializer(typeof(T));

                    // Use the XmlSerializer object to deserialize the data from the file
                    ret = (T)xs.Deserialize(fs);
                }
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                throw new Exception("Could not find the data directory '" + fileName + "'");
            }
            catch (System.IO.FileNotFoundException)
            {
                ret = new T();
            }

            return ret;
        }

        public static void Save(T config)
        {
            Save(config, ConfigFile);
        }

        public static void Save(T config, string fileName)
        {
            // Create file to save the data to

            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {

                // Create an XmlSerializer object to perform the serialization
                XmlSerializer xs = new XmlSerializer(typeof(T));

                // Use the XmlSerializer object to serialize the data to the file
                xs.Serialize(fs, config);
            }
        }

        static private string configFile = null;
        static public string ConfigFile
        {
            get
            {
                if (configFile == null)
                {
                    string appName = typeof(T).FullName;

                    string baseFolder = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    string jFolder = Path.Combine(baseFolder, "JToolsConfig");
                    string dataPath = Path.Combine(jFolder, appName);

                    if (!Directory.Exists(dataPath))
                    {
                        Directory.CreateDirectory(dataPath);
                    }

                    configFile = Path.Combine(dataPath, FILENAME);
                }

                return configFile;
            }
        }

    }
}
Eric J.
The Local Application Data seems like a great idea but I can't seem to find much documentation on how to correctly implement it, unless I'm using a poor google search.
Jamie Keeling
@Jamie: Look at the accepted answer to http://stackoverflow.com/questions/1992905/c-error-creating-directory-in-specialfolder-localapplicationdata-on-windows-7-as You get a path using the method outlined there, then write a file to that path. As Oded pointed out, you can use XmlSerializer (for example) to save and load the data. I'll post a code example in my answer in a moment.
Eric J.
Thanks for the snippet and link to the other question, I'll be sure to use the special folders approach with the suggestions in the other answers.
Jamie Keeling
+2  A: 

The approach I would take is to have a Locations class (and collection), which I could serialize into an XML file (using the XmlSerializer or DataContractSerializer).

You can check for the existence of the XML file - if it exists deserialize it to your collection, if it doesn't you can create a new collection with your hard-coded defaults.

Oded
I think this would be the best solution in combination with the solution proposed by Shawn, by allowing the user to specify the location of an XML file it might be easier for them to use rather than force them to have it in a certain location.
Jamie Keeling
+1  A: 

I suggest you organize all your data in a user-readable way and ask the user where he wants it stored on his computer (using one or many XML files). This way the user can store the XML files wherever he likes and understand what they do. He can use them to share his "library" with others, he can apply changes to these XML files manually, he could even make his own program which uses the same file (some kind of add-on to your own program).

Shawn
Good idea, allowing the user the choice of destination means they're not too restricted.
Jamie Keeling