我正在设计一个应用程序,允许您查找某些程序制作的图片(屏幕截图).我将在应用程序本身中提供一些程序的位置以启动用户. 我想知道随着时间的推移我应该如何添加新位置,我的第一个
我想知道随着时间的推移我应该如何添加新位置,我的第一个想法就是将其硬编码到应用程序中,但这意味着用户必须重新安装才能使更改生效.
我的第二个想法是使用XML文件来包含所有位置以及其他数据,例如应用程序的名称.这也意味着用户可以根据自己的意愿添加自己的位置,也可以通过互联网分享.
第二个选项似乎是最好的方法,但后来我不得不考虑如何在用户计算机上管理它.理想情况下,我只想要一个.exe而不依赖于任何外部文件,例如XML,但这会让我回到第一点.
是否最好只使用ClickOnce部署在开始菜单中创建一个条目并创建一个包含.exe和文件名的文件夹?
感谢您的反馈,我不想在设计被钉牢之前开始实施应用程序.
通常我不会硬编码应用程序中的任何内容.正如你正确指出的那样,这就是一种不灵活的设计.我通常将此类配置信息存储在Local Application Data文件夹下的XML文件中.
如果您的用户始终(或经常)与互联网连接,您可以考虑在Web服务上存储此类“书签”信息的替代(或其他)功能.
编辑:
这是我多年来用于此类事情的代码片段
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;
}
}
}
}
