开发者

Where can I safely store data files for a ClickOnce deployment?

I have been using ApplicationDeployment.CurrentDeployment.DataDirectory to store content downloaded by the client at runtime which is expected to be there every time the app launches, however now I've found this changes seemingly randomly if the application is updated.

What is the best reliable method for storing user data for the application in click-once deployments?

Currently I've been using the following method

private const string LocalPath = "data";

public string GetStoragePath() {
    string dir;
    if (ApplicationDeployment.IsNetworkDeployed) {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
        dir = Path.Combine(ad.DataDirecto开发者_StackOverflowry, LocalPath);
    } else {
        dir = LocalPath;
    }
    return CreateDirectory(dir);
}

I originally followed the article Accessing Local and Remote Data in ClickOnce Applications under the heading ClickOnce Data Directory which states this is recommended path.

NOTE: CreateDirectory(string) simply creates a directory if it doesn't already exist.

I have found the root cause of my problem is I'm creating many files and an index file, this index file contains absolute paths, click-once moves the content (or copies) on an upgrade, so the absolute paths no longer exist. I will investigate isolated storage as Damokles suggests to see if this has the same side affect for click-once deployments.


Another option is to make a directory for your application in the user's AppData folder and store it there. You can get a path to that with this:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

You'll find a lot of applications use that (and it's local equivalent). It also doesn't move around between ClickOnce versions.


Check out IsolatedStorage this should help. It even works in partial trust environments.

To keep you data you need to use the application scoped IsolatedStorage

using System.IO;
using System.IO.IsolatedStorage;
...

IsolatedStorageFile appScope = IsolatedStorageFile.GetUserStoreForApplication();    
using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream("data.dat", FileMode.OpenOrCreate, appScope))
{
...

code taken from this post


It depends on the data you are saving.

You are currently saving to the Data Directory which is fine. What you need to be aware of is that each version of the application has its own Data Directory. When you update ClickOnce copies all the data from the previous version to the new version when the application is started up. This gives you a hook to migrate any of the data from one version to the next. This is good for in memory databases like Sql Lite or SQL CE.

One thing that I cam across is that when you have a large amount of data (4 gig) if you store it in the Data Directory this data will be copied from the old version to the new version. This will slow down the start up time after an upgrade. If you have a large amount of data or you don't want to worry about migrating data you can either store the data in the users local folder providing you have full trust or you can use isolated storage if you have a partial trust.

Isolated Storage

Local User Application Data

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜