Doc Roms
Doc Roms

Reputation: 3308

How to delete all data when uninstalling a ClickOnce application?

I have a ClickOnce application in C#, and I want to uninstall all files during an automatic uninstall.

Previously I've used the following to return my Application Data Path :

static public string APPLICATION_DATA_PATH
{
        get
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString(), "myFolder\\");
        }
} 

This returns c://Users/UserName/AppData/MyFolder which works but, with automatic uninstall of a ClickOnce application, this folder isn't deleted.

Now, I test with :

static public string APPLICATION_DATA_PATH
{
    get
    {
        if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
        {
            // If Clickonce Application is deployed.
            return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory
        } 
        else
        {
            // If I'm in debug mode (with visual studio)
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString(), "myFolder\\");
        }
    }
} 

That works with Visual Studio debugging but, when I publish my application, and I install on any computer, my application crashes and the Windows Report (WER) just says "APPLICATION CRASH".

I've got no idea why my this doesn't work if my APPLICATION_DATA_PATH is correct...

Have you any idea for debugging a ClickOnce application using System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory path? Or an idea for removing all files with ClickOnce uninstaller?

EDIT

I arrived to debug my ClickOnce Application (thx @JRoughan for help), Now, I saw My "APPLICATION_PATH" return null, I've change my code :

static public string APPLICATION_DATA_PATH
    {
        get
        {
            string path = null;                
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory;
            }

            return path;
        }
    }

If null is return, Code don't approuved the "System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed", but why? I've install a new clickonce application... and the application is deployed, the code should return the "CurrentDeployment.DataDirectory"! no ?

FINAL EDIT

Finally, the path works correctly. On the start of my application, I restart here, if application don't have the administrator rules, but, when Clickonce Application have Administrator rules, the path don't work... Thanks for help!

Upvotes: 0

Views: 1786

Answers (2)

RobinDotNet
RobinDotNet

Reputation: 11877

There is no mechanism in ClickOnce deployment to perform any kind of tasks when you uninstall the application. In theory, the ClickOnce scavenger will come around eventually and take care of the files, but I find this to be unreliable. Basically, there's nothing you can do about this short of installing and running a program to clear the \apps\2.0\data\whatever folder in question.

Upvotes: 0

JRoughan
JRoughan

Reputation: 1655

If your question is how to debug an installed ClickOnce application so you can see exactly what's happening you should be able to attach the debugger to the process using the VS menu 'Debug -> Attach to Process'. Make sure the right solution is loaded first.

If you're just asking why the data directory isn't being cleaned correctly, I'm not sure; This folder should follow the lifetime of the ClickOnce install and be cleaned up during uninstall.

Is it possible you're loading resources from this folder and not releasing them so they're locked and undeletable at the time of uninstall?

Upvotes: 1

Related Questions