3

How to check if application is running as UWP?

Almost the same question, but I need it for C#, not C++.

If I try to get ApplicationData.Current.LocalFolder and application is running as Win32 application, it throws exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in applcation.exe Additional information: The process has no package identity. (Exception from HRESULT: 0x80073D54) occurred

How can I check type of application without exception?

Alexan
  • 8,165
  • 14
  • 74
  • 101
  • There just isn't any way that you can't know when you sit in front of your machine. You have to pick the right project template, maybe enable the desktop bridge, upload the app to the store. An obvious thing to do, while you sit in front of that machine, is to edit Project > Properties > Build tab > Conditional compilation symbols setting. – Hans Passant Dec 18 '17 at 22:55
  • @HansPassant, actually, what I'm trying to do: [almost the same question](https://stackoverflow.com/questions/39609643/determine-if-c-application-is-running-as-a-uwp-app-in-desktop-bridge-project) but only for C# – Alexan Dec 18 '17 at 23:10
  • Be sure to not mention that you use the bridge at all, you don't want to make it easy for anybody to help you. – Hans Passant Dec 18 '17 at 23:34
  • @HansPassant, changed title and added tag – Alexan Dec 18 '17 at 23:48
  • You can P/Invoke the API listed in the related question. – Peter Torr - MSFT Dec 19 '17 at 05:10
  • @PeterTorr-MSFT, but, there is no way to use .NET way? – Alexan Dec 19 '17 at 05:32
  • `ApplicationData.Current.LocalFolder` is impossible on win32? – Vitaly Zdanevich Apr 26 '19 at 14:08

3 Answers3

5

You can call the GetCurrentPackageFullName method in C# using P/Invoke. There is a DesktopBridge.Helpers package that you can use to do this for you. It also works on Windows 7.

DesktopBridge.Helpers helpers = new DesktopBridge.Helpers(); 
bool isUwp = helpers.IsRunningAsUwp(); 

You can see how it is implemented here if you are interested: https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs

And here is the Nuget package: https://www.nuget.org/packages/DesktopBridge.Helpers/

mm8
  • 163,881
  • 10
  • 57
  • 88
  • yes, it's written in this [article](https://blogs.msdn.microsoft.com/appconsult/2016/11/03/desktop-bridge-identify-the-applications-context/), link from [this answer](https://stackoverflow.com/a/47882955/240564) – Alexan Dec 19 '17 at 16:38
  • "using this code on Windows 7, Vista or even XP will always return an exception", but we need Win7 – Alexan Dec 19 '17 at 16:50
  • IsRunningAsUwp() returns false on Windows 7 and lower. It doesn't throw. Didn't you try? – mm8 Dec 19 '17 at 16:51
1

Have you seen this MSDN article? Desktop Bridge – Identify the application’s context

It has a sample that shows how make the app behave differently, depending the running mode (win32 desktop application or desktop bridge app).

Bogdan Mitrache
  • 10,536
  • 19
  • 34
  • from this article: I need to use P/Invoke or use their nuget, which uses P/Invoke, there is not .NET API for this – Alexan Dec 19 '17 at 16:34
  • No, GetCurrentPackageFullName is an unmanaged method. There is no .NET equivalent. But again, you can use P/Invoke to call it. – mm8 Dec 19 '17 at 16:40
0

I added a nuget reference to the OSVersionHelper package and then used this code to determine if the app was running as a packaged app using Desktop Bridge:

bool isRunningAsPackagedApp = OSVersionHelper.WindowsVersionHelper.HasPackageIdentity;
Matt Varblow
  • 7,651
  • 3
  • 34
  • 44