Earlz
Earlz

Reputation: 63905

Programmatically matching a package name to a full package name

I'm trying to get a good way of automatically running appcert.exe in an easy way.

I can not for the life of me figure out how to get the full package name of a package. For instance, I have an application titled TwitterDemo, yet when I run Get-AppxPackage, that application doesn't even appear to be listed(probably listed as something like c2934-289aa9-394.... Also, I really had rather not rely on using Get-AppxPackage from powershell and then parsing it's output and/or moving it to regular command line. There doesn't even appear to be an API for querying the package database from .Net, and the C++ API seems rather complex.

So basically, what's the easiest way of matching an applications "title" to it's full package name?

Upvotes: 1

Views: 2104

Answers (3)

Howard Kapustein
Howard Kapustein

Reputation: 527

There are multiple ways to determine the Package Full Name for a package. Do you have a running process, an installed package or merely definitional information?

If you have a running process, GetPackageFullName() is the best way.

If you have an installed package, Powershell is probably the most convenient for human. To list all packages installed for the current user: powershell -c $(Get-AppxPackage).PackageFullName You can do the equivalent programmatically via PackageManager's FindPackages() method.

If you just know a package's identity you can calculate its Package Full Name via PackageFullNameFromId()

Upvotes: 1

Earlz
Earlz

Reputation: 63905

I found the easiest way is to use the Windows.Management.Deployment.PackageManager. This doesn't completely solve the problem, but I think my design was a bit too broad in scope.

Upvotes: 0

John Koerner
John Koerner

Reputation: 38087

I am assuming you are writing an outside app to automate appcert. I am also assuming that this is a win32 app. If this is the case, you can use the win32 API GetPackageFullName to get the full name a pacakge, given a process handle. So in the example below, I added a listbox and button to an app and then have the following code:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetPackageFullName(IntPtr hProcess, ref UInt32 packageFullNameLength, StringBuilder fullName);

public Form1()
{
    InitializeComponent();
    listBox1.DataSource = System.Diagnostics.Process.GetProcesses();

    listBox1.DisplayMember = "ProcessName";
}


private void button1_Click(object sender, EventArgs e)
{
    var proc = listBox1.SelectedItem as System.Diagnostics.Process;
    uint len = 250;
    StringBuilder sb = new StringBuilder(len);
    var err =  GetPackageFullName(proc.Handle, ref len, sb);

    MessageBox.Show(sb.ToString());     
}

Upvotes: 0

Related Questions