开发者

Where should I store my application data?

开发者 https://www.devze.com 2022-12-28 22:39 出处:网络
I have an application that needs to store data.Currently, I am using the built-in Application Settings to do it, but it only gives me two choices: application and user scopes.Ideally, I want a \"local

I have an application that needs to store data. Currently, I am using the built-in Application Settings to do it, but it only gives me two choices: application and user scopes. Ideally, I want a "local" scope that allows the application to开发者_如何学编程 run under another user and still find its data rather than recreate it for that user. The application scope can do this, but it's read only. The application data will be changed by the user. It's OK if only the administrator is allowed to make changes to the data.

As you probably can guess, I have an administration tool that allows the user to change the data and windows service runner that reads the data and does something with it. It would be great if the windows service runner access the data created by the administration tool.


If the data is very, very simple, and you need it to be readable by other applications or users (with appropriate permissions), I would probably choose to store it in an XML file or even a plain-text file inside the user's Application Data folder, which would be obtained via Environment.GetFolderPath. An example for saving might look like:

using System.IO;
using System.Xml.Linq;

string settingsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(settingsDirectory))
    Directory.CreateDirectory(settingsDirectory);
string fileName = "tasks.xml";
string settingsPath = Path.Combine(settingsDirectory, fileName);
XDocument settingsDoc = new XDocument(
    new XElement("Tasks",
        new XElement("Task",
            new XElement("Name", "Make Breakfast"),
            new XElement("Location", @"C:\Program Files\MyApp\Plugins"),
            new XElement("FileName", "breakfast.dll"))));
// ... etc.
settingsDoc.Save(settingsPath);

That's it - settings saved! You can load them again with XDocument.Load.


Sounds like you want to store it in a database, the question is local or on a network or not. The answer also depends on what kind of data you are storing, how your app is distributed, and other factors.

Oh, and BTW we could help you way better if you specify your platform (preferably with a tag)--silverlight, wpf, winforms, asp.net, console, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消