How to find and list all UWP apps / packages? - c#

My goal is to find and launch a UWP app by name (e.g. Twitter). I'm currently using an elevated desktop extension, following a guide by Stefan Wick.
In my full-trust Win32 console process, I'm currently using the PackageManager to find and list all the UWP apps, and it works on my machine. However, when I send my finalized app package to another user, nothing appears on his screen, even after running elevated.
Here's my current code:
var PkgMgr = new PackageManager();
var currUserPkgs = PkgMgr.FindPackagesForUser(string.Empty);
foreach (Package pkg in currUserPkgs)
{
string pkgName = pkg.DisplayName;
if (pkgName == "")
{
continue;
}
if (pkgName.Contains(appName) || appName.Contains(pkgName) ||
percentSimilarity(appName, pkgName) >= 0.50)
{
// we found it
appPkgName = pkg.Id.FamilyName;
break;
}
}
Why does this not bring up any packages on another user's machine? There's no error message that's called.
Also, is there another solution that can locate all UWP packages? Thank you!

Related

Windows 10 UWP App Launch from Package List (FindPackage)

My goal is to launch a file from an app that the user specifies in my UWP app. When I hit on the Launch button, I navigate backwards into my AppData (UWP) Packages folder, then I iterate through folder and utilize the PackageManager class to retrieve packages by name. I try to initialize a package with the right package name using FindPackage (here). However, it doesn't work for me and I get an exception "Value does not fall within the expected range" when calling FindPackage.
Here's what I've tried so far:
StorageFolder currApp = Windows.Storage.ApplicationData.Current.LocalFolder;
Debug.WriteLine(currApp.Path);
// move up and get parent folder for all packages
DirectoryInfo currAppFolder = Directory.GetParent(currApp.Path);
DirectoryInfo pkgs = Directory.GetParent(currAppFolder.FullName);
StorageFolder pkgFldr = await StorageFolder.GetFolderFromPathAsync(pkgs.FullName);
var pkgDirs = await pkgFldr.GetFoldersAsync();
Package UwpPkg = null;
var PkgMgr = new PackageManager();
foreach (StorageFolder dir in pkgDirs)
{
string folderName = dir.Name;
var currPkg = PkgMgr.FindPackage(folderName);
if (filter == currPkg.DisplayName)
{
// we found it
UwpPkg = currPkg;
break;
}
}
I've also already configured necessary permissions (rescap: broadFileSystemAccess).
Did someone also have such problems/a possible solution? Thank you!
Windows 10 UWP App Launch from Package List (FindPackage)
Please check PackageManager document, for this api it need packageQuery capability
<Package
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap" >
......
<rescap:Capability Name="packageQuery" />
And please note
This method requires administrative privileges. The returned package may be installed for the current user or for another user. So please invoke above in the elevated desktop extension. For more you could stefan's blog App Elevation Samples.

Reboot/Restart an UWP app

I have an UWP app (published in Windows/Microsoft Store), and I am working in a new update, and I use Template10 in my app, that has dark and light theme, and in Windows 10 Mobile but for the change to be effective, the user has to manually close the app and restart it.
Is there any possibility to restart/reboot my application? That is, to close the application alone/automatically and reopen my application automatically?
With the Fall Creators Update (1709) We have introduced a new method on CoreApplication called RequestRestart() that enables this scenario. You will need the 16299 SDK (or later) to access this API.
https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.core.coreapplication#Methods_
Here is a blog/sample:
https://blogs.windows.com/buildingapps/2017/07/28/restart-app-programmatically/
You can do this with CoreApplication.RequestRestart
var result = await CoreApplication.RequestRestartAsync("Application Restart Programmatically ");
if (result == AppRestartFailureReason.NotInForeground ||
result == AppRestartFailureReason.RestartPending ||
result == AppRestartFailureReason.Other)
{
var msgBox = new MessageDialog("Restart Failed", result.ToString());
await msgBox.ShowAsync();
}

Referencing WinRT/UWP libraries in a .NET desktop application while maintaining support for Windows 7

I am trying to reference "Windows.Networking.Connectivity" classes in my desktop application. I am basically interested in handling metered connections in my app.
Basically what I am trying to do is simple:
var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if (connectionCost.NetworkCostType == NetworkCostType.Unknown
|| connectionCost.NetworkCostType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}
The only method I know of that allows a desktop application to reference UWP assemblies is by manually editing the project file and adding the following line to the csproj file:
<TargetPlatformVersion>8.0</TargetPlatformVersion>
Applying the code and "hack" works fine but the problem is that doing so will prevent my app from running on Windows 7 which I need to support.
I was wondering if there is a way to reference UWP assemblies in a desktop application without having to drop support for Windows 7.
And since for the time being I only want to check if a connection is metered, I am open to suggestions about how to get this information without referencing Windows assemblies.
I found a way to use reflection and call UWP methods without having to specify a target platform. For my case this is what I did:
var networkInfoType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileType = Type.GetType("Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType=WindowsRuntime");
var profileObj = networkInfoType.GetTypeInfo().GetDeclaredMethod("GetInternetConnectionProfile").Invoke(null, null);
dynamic profDyn = profileObj;
var costObj = profDyn.GetConnectionCost();
dynamic dynCost = costObj;
var costType = (NetworkCostType)dynCost.NetworkCostType;
if (costType == NetworkCostType.Unknown
|| costType == NetworkCostType.Unrestricted)
{
//Connection cost is unknown/unrestricted
}
else
{
//Metered Network
}

How can I determine bandwidth on Windows 7, 8.1 and 10?

So far I have struggled to get MbnInterfaceManager working (see hresult from IMbnInterfaceManager::GetInterfaces when no MBN device exists), so instead I built and debugged an application with no problems from within Visual Studio 2015 that executed this WMI query in C# (see also the Win32_PerfFormattedData_Tcpip_NetworkInterface documentation):
string query = "SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
But then when I deployed the application to Windows 8.1, I receive this error every time the query is executed:
System.Management.ManagementException: Invalid query
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
Does anyone have any suggestions on how to resolve this issue? How can I deploy an application so that it is able to use queries like this?
UPDATE:
Please note that I can build and run the above code (as part of a larger WPF application) from within Visual Studio 2015 on either Windows 7 or Windows 8.1, and I can deploy the same application using ClickOnce onto Windows 7 where it runs successfully. For some reason when I deploy this application using ClickOnce onto Windows 8.1, I get that Invalid query message.
I think what I have to do is make sure that the System.Management reference is set to "Copy Local" but I'm not able to test that right now. If anyone has any better ideas please feel free to let me know.
UPDATE:
It is not possible to use System.Management.dll on Windows 8.1 in the same way it is used on Windows 7 or Windows 10.
I've found that to perform operations similar to the ones I mentioned in my question on Windows 8.1 and Windows 8 phone you need to either get a Windows 8.1 developer license or on Windows 10 set your computer to "Developer Mode" so you can use the Windows.Networking.Connectivity namespace:
string connectionProfileInfo = string.Empty;
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
}
else
{
connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
OutputText.Text = connectionProfileInfo;
rootPage.NotifyUser("Success", NotifyType.StatusMessage);
}
// Which calls this function, that allows you to determine how strong the signal is and the associated bandwidth
string GetConnectionProfile(ConnectionProfile connectionProfile)
{
// ...
if (connectionProfile.GetSignalBars().HasValue)
{
connectionProfileInfo += "====================\n";
connectionProfileInfo += "Signal Bars: " + connectionProfile.GetSignalBars() + "\n";
}
// ...
}
Please note that you have to make sure your project is either a Window 8.1 PCL or a Windows 8.1 app to be able to reference the namespace.
For details please see https://code.msdn.microsoft.com/windowsapps/network-information-sample-63aaa201
UPDATE 2:
To be able to get bandwidth on Windows 7, 8.1 and 10, I ended up using this code:
private int GetMaxBandwidth()
{
int maxBandwidth = 0;
NetworkInterface[] networkIntrInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var networkInterface in networkIntrInterfaces)
{
IPv4InterfaceStatistics interfaceStats = networkInterface.GetIPv4Statistics();
int bytesSentSpeed = (int)(interfaceStats.BytesSent);
int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived);
if (bytesSentSpeed + bytesReceivedSpeed > maxBandwidth)
{
maxBandwidth = bytesSentSpeed + bytesReceivedSpeed;
}
}
}

struggling with mobile broadband api windows 7 and windows 8 with C#, not sure what to install

I have an application that requires to control mobile broadband API.
I am struggling on correctly installing the api on my devices.
I've been follow the instructions in this document:
http://www.google.be/url?sa=t&rct=j&q=&esrc=s&frm=1&source=web&cd=1&cad=rja&ved=0CC0QFjAA&url=http%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F7%2FE%2F7%2F7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2%2FMB_ManagedCode.docx&ei=kyvmUs7jE4e60QWbooHYDg&usg=AFQjCNG6yaGf4sRhdbWI99fE7tmQX8cmnA&sig2=2Fg-_DRYBIselKR19wTq2Q
and trying to combine the steps with this stackoverflow explanation
C# Read Windows Mobile Broadband connection properties
I have been able to lay a reference from visual studio to mbnapi.tlb in V7.0/lib. and I automatically now have a interop.mbnapi.tlb in my obj/debug folder.
When trying to "check the SIM is inserted and working / activated". => my code crashes on the following line
IMbnInterface[] mobileInterfaces = mbnInfMgrInterface.GetInterfaces() as IMbnInterface[];
When I run it on windows 8, mbnInfMgrInterface == null
I have already tried to install the same SDK on windows 8 as stated in the requirements of the document but the SDK is only meant for windows 7...
I have tried to register the mbnapi in windows 8 by performing
Regtlibv12 Mbnapi.tlb
no luck whatsoever...
what do I need to do to get this to work please?
anyone has some experience in this?
EDIT. on windows 7 (my development machine), I get the message "Device not ready", I think this is normal because I don't have mobile broadband on it, on windows 8 I do, but there the mobile interface manager is null => mbnInfMgrInterface == null.
thank you,
Not sure exactly what you are after, but after struggling with IMbnInterface and GetSignalStrength() (see https://msdn.microsoft.com/en-us/library/windows/desktop/dd323166(v=vs.85).aspx) and being unsuccessful, I found that you can obtain a lot of info using WMI:
int maxBandwidth = 0;
string query = "SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
{
if (Convert.ToInt32(mo["CurrentBandwidth"]) > maxBandwidth)
{
// Instead of CurrentBandwidth you may want to use BytesReceivedPerSec
maxBandwidth = Convert.ToInt32(mo["CurrentBandwidth"]);
}
}
Please see answer here: Determining the network connection link speed and here is the list of properties you can obtain: https://msdn.microsoft.com/en-us/library/aa394293(VS.85).aspx
UPDATE:
Please note that I can build and debug the above code (as part of a larger WPF application) from within Visual Studio 2015 on either Windows 7 or Windows 8.1, and I can deploy the same application onto Windows 7 where it runs successfully. For some reason when I deploy this application on Windows 8.1, I get an Invalid query message.
UPDATE 2:
Please note that I found you cannot get the network info in Windows 8.1 in the same way as you do in Windows 7, in that the System.Management namespace is not available on Windows 8.1. See https://code.msdn.microsoft.com/windowsapps/network-information-sample-63aaa201
string connectionProfileInfo = string.Empty;
ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (InternetConnectionProfile == null)
{
rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
}
else
{
connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
OutputText.Text = connectionProfileInfo;
rootPage.NotifyUser("Success", NotifyType.StatusMessage);
}
// Which calls this function, that allows you to determine how strong the signal is and the associated bandwidth
string GetConnectionProfile(ConnectionProfile connectionProfile)
{
// ...
if (connectionProfile.GetSignalBars().HasValue)
{
connectionProfileInfo += "====================\n";
connectionProfileInfo += "Signal Bars: " + connectionProfile.GetSignalBars() + "\n";
}
// ...
}

Categories

Resources