Windows应用程序的设计问题,最佳方法?

时间:2021-03-17 15:51:01

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.

我的第二个想法是使用XML文件来包含所有位置以及其他数据,例如应用程序的名称。这也意味着用户可以根据自己的意愿添加自己的位置,也可以通过互联网分享。

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.

第二种选择似乎是最好的方法,但后来我不得不考虑如何在用户计算机上进行管理。理想情况下,我只想要一个.exe而不依赖于任何外部文件,例如XML,但这会让我回到第一点。

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?

是否最好只使用ClickOnce部署在开始菜单中创建一个条目并创建一个包含.exe和文件名的文件夹?

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

感谢您的反馈,我不想在设计被钉牢之前开始实施应用程序。

3 个解决方案

#1


3  

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.

我通常将此类配置信息存储在Local Application Data文件夹下的XML文件中。

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.

如果您的用户始终(或经常)与互联网连接,您可以考虑在Web服务上存储此类“书签”信息的替代(或其他)功能。

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;
            }
        }

    }
}

#2


2  

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).

我将采用的方法是使用Locations类(和集合),我可以将其序列化为XML文件(使用XmlSerializer或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.

您可以检查XML文件是否存在 - 如果存在,则将其反序列化到您的集合中,如果不存在,则可以使用硬编码的默认值创建新集合。

#3


1  

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).

我建议您以用户可读的方式组织所有数据,并询问用户他希望将其存储在计算机上的位置(使用一个或多个XML文件)。这样,用户可以将XML文件存储在他喜欢的任何地方,并了解他们的工作。他可以使用它们与他人共享他的“库”,他可以手动对这些XML文件应用更改,他甚至可以创建自己的程序,使用相同的文件(某种附加到您自己的程序)。

#1


3  

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.

我通常将此类配置信息存储在Local Application Data文件夹下的XML文件中。

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.

如果您的用户始终(或经常)与互联网连接,您可以考虑在Web服务上存储此类“书签”信息的替代(或其他)功能。

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;
            }
        }

    }
}

#2


2  

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).

我将采用的方法是使用Locations类(和集合),我可以将其序列化为XML文件(使用XmlSerializer或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.

您可以检查XML文件是否存在 - 如果存在,则将其反序列化到您的集合中,如果不存在,则可以使用硬编码的默认值创建新集合。

#3


1  

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).

我建议您以用户可读的方式组织所有数据,并询问用户他希望将其存储在计算机上的位置(使用一个或多个XML文件)。这样,用户可以将XML文件存储在他喜欢的任何地方,并了解他们的工作。他可以使用它们与他人共享他的“库”,他可以手动对这些XML文件应用更改,他甚至可以创建自己的程序,使用相同的文件(某种附加到您自己的程序)。