ClickOnce application: test if program is being run off line

robertb_NZ

Well-known member
Joined
May 11, 2010
Messages
146
Location
Auckland, New Zealand
Programming Experience
10+
I have a ClickOnce application published with option "The application is available offline as well". When run from the web page it is passed a URI query, so I need to know if it is being run offline, or from Visual Studio. I have the following code, which works, but is there a better way that doesn't use Try/Catch, to avoid relying on error trapping for "normal logic".

If System.Diagnostics.Debugger.IsAttached Then ' Case 1: Debugging (run from Visual Studio)
' Set Debugging defaults​
Else
Try ' Case 2. Run from JazzSoftware web page.
QueryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query
HandleParameters() ' Parameter received with download
' Encrypt and save query string
Catch ex As Exception ' Case 3. Run downloaded copy offline
QueryString = Decrypt(saved query string)
HandleParameters()
End Try​
End If

Thank you, Robert.
 
What's the exception that gets thrown? I don't really use ClickOnce so I don't know the answer to your question off the top of my head but the nature of the exception may lead to an answer.
 
Thanks John, that was the right question. The answer: Unhandled exception, caused by "Object reference not set to an instance of an object".

Which got me thinking: the problem was clearly somewhere in the ApplicationDeployment.CurrentDeployment object. So I put in a diagnostic: -
If IsNothing(ApplicationDeployment.CurrentDeployment) Then
MsgBox("#1")​
ElseIf IsNothing(ApplicationDeployment.CurrentDeployment.ActivationUri) Then
MsgBox("#2")​
ElseIf IsNothing(ApplicationDeployment.CurrentDeployment.ActivationUri.Query) Then
MsgBox("#3")​
End If

The test => "#2".

So I was able to fix the problem like this: -
If System.Diagnostics.Debugger.IsAttached Then ' Case 1: Debugging (run from Visual Studio)
' Code for debugging​
ElseIf IsNothing(ApplicationDeployment.CurrentDeployment) OrElse IsNothing(ApplicationDeployment.CurrentDeployment.ActivationUri) _
OrElse IsNothing(ApplicationDeployment.CurrentDeployment.ActivationUri.Query) Then​
' Case 3. Run downloaded copy offline. This is the code that was previously in the Catch​
Else
' Case 2. Run from JazzSoftware web page.​
End If

I really only need the 2nd condition in
ElseIf IsNothing(...) OrElse IsNothing(...) OrElse IsNothing(...) Then​
However I thought that it would be safer to check each level of the hierarchy in case I don't understand enough about what's really going on.

Thank you, Robert.
 

Latest posts

Back
Top