Welcome to MSDN Blogs Sign in | Join | Help

Detecting that You're Running in a ClickOnce Application

In my last post,  I mentioned that application scoped isolated storage only works if you're running in a ClickOnce application.  That begs the question -- how do I tell if I'm currently running in the context of a ClickOnce application?

You can see if a ClickOnce application is running in the current AppDomain by checking the AppDomain.CurrentDomain.ActivationContext property.  If that value is non-null, then the domain is running a ClickOnce application.  The ActivationContext will also get you the name of that application.

What you choose to do with that information is up to you.  In general, modifying your behavior depending on the context of the application leads to difficult to test and debug programs.  However, there may be situations (such as choosing an isolated storage scope), where having this information is necessary and useful.

Published Friday, January 20, 2006 7:00 AM by shawnfa
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Detecting that You're Running in a ClickOnce Application

Friday, January 20, 2006 5:43 PM by Neno Loje
How about using System.Deployment.Application.ApplicationDeployment.CurrentDeployment.IsNetworkDeployed

MSDN Library states: "Gets a value indicating whether the current application is a ClickOnce application."

# re: Detecting that You're Running in a ClickOnce Application

Friday, January 20, 2006 7:31 PM by shawnfa
Hi Neno,

Actually, the reason behind this post was someone attempting to use the ApplicationDeployment.CurrentDeployment property :-)

If you access CurrentDeployment when not running as a ClickOnce application, the property will throw an InvalidDeploymentException.

-Shawn

# re: Detecting that You're Running in a ClickOnce Application

Tuesday, January 24, 2006 7:57 PM by Neno Loje
Interesting fact. :-)

But what sence does "IsNetworkDeployed" make then...?

-Neno

# re: Detecting that You're Running in a ClickOnce Application

Friday, January 27, 2006 11:25 AM by shawnfa
It could detect if your ClickOnce application was installed off of a network location or installed off of a CD-ROM for instance.

-Shawn

# Partial Trust and Click Once

Monday, January 30, 2006 7:08 AM by Dinis Cruz @ Owasp .Net Project

Find
Out What's New with Code Access Security in the .NET Framework 2.0
and its
side notes (MSDN...

# re: Detecting that You're Running in a ClickOnce Application

Thursday, February 02, 2006 8:55 AM by Richard
The IsNetworkDeployed property is static, which means you don't need to access the CurrentDeployment property first. The documentation is correct - it returns true if the application is a ClickOnce application, and false otherwise. It has nothing to do with UNC vs CD deployment. However, looking at the IL for the property, it works by accessing the CurrentDeployment property and catching the InvalidDeploymentException. This would be quite an expensive call in a non-deployed application. Checking for a null ActivationContext is a much more efficient test. Perhaps the ApplicationDeployment class should be rewritten to avoid the exception? Something like this should do it: [PermissionSet(SecurityAction.Assert, Name="FullTrust")] private static ApplicationDeployment TryGetCurrentDeployment(out bool requireDemand) { requireDemand = false; if (null == _currentDeployment) { lock (lockObject) { if (null == _currentDeployment) { string appIdentity = null; ActivationContext context = AppDomain.CurrentDomain.ActivationContext; if (null != context) appIdentity = context.Identity.FullName; if (!string.IsNullOrEmpty(appIdentity)) { _currentDeployment = new ApplicationDeployment(appIdentity); requireDemand = true; } } } } return _currentDeployment; } public static ApplicationDeployment CurrentDeployment { get { bool requireDemand; ApplicationDeployment result = TryGetCurrentDeployment(out requireDemand); if (null == result) throw new InvalidDeploymentException(Resources.GetString("Ex_AppIdNotSet")); if (requireDemand) result.DemandPermission(); return result; } } public static bool IsNetworkDeployed { get { bool requireDemand; ApplicationDeployment result = TryGetCurrentDeployment(out requireDemand); if (null == result) return false; if (requireDemand) result.DemandPermission(); return true; } }

# re: Detecting that You're Running in a ClickOnce Application

Saturday, February 11, 2006 4:00 PM by Neno Loje
Hmm... the following works perfectly:

if (!ApplicationDeployment.IsNetworkDeployed)
{
 // ... and show warning if its not and abort startup...
 MessageBox.Show(Properties.Resources.NotNetworkDeployed, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 return;
}

# re: Detecting that You're Running in a ClickOnce Application

Monday, February 20, 2006 2:23 PM by Michele Leroux Bustamante
IsNetworkDeployed returns false when running in debug, true if run via the application launcher (program group). As you would expect.

ActivationContext returns true even in debug mode...once you publish the application the first time. Even if you do not configure a URL to launch.

I can't use ActivationContext to verify the current deployment, because it fails if not network deployed:

ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;

# re: Detecting that You're Running in a ClickOnce Application

Wednesday, February 22, 2006 12:26 PM by shawnfa
ActivationContext will be non-null in any situation that the AppDomain is configured to run a ClickOnce application.  (Or, in the future, any manifested style application).

-Shawn

# Detecting if Running as ClickOnce Application

Monday, February 27, 2006 4:27 PM by Neno Loje's Treasury
How do you detect if you are running as a ClickOnce application?
ActivationContext
Well, use can use...

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker