如何在 Powershell 中引用 WinRT 组件
How to reference WinRT components in Powershell
我正在尝试在 PowerShell 中使用 Windows.System
命名空间,它是 WinRT 的一部分。
程序集不是 dll
,而是 winmd
。两种加载程序集到PowerShell的方法好像都不行
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Windows Kits.0\References\CommonConfiguration\Neutral\Windows.winmd")
#Add-Type -Path "C:\Program Files (x86)\Windows Kits.0\References\CommonConfiguration\Neutral\Windows.winmd"
我知道使用 Windows API 比加载我的 .NET 代码要复杂一些。我有 pinched 使用部分 win32 的代码,WinRT 解决方案是否类似?
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"@
}
Register-NativeMethod "user32.dll" "int MessageBox (IntPtr hWnd, string text, string caption,int type)"
Add-NativeMethods
[NativeMethods]::MessageBox(0, "Please do not press this again.", "Attention", 0)| Out-Null
我的目标是在 PowerShell 中 运行 Windows.System.Launcher.LaunchFileAsync("C:\file");
。如何加载我需要的 WinRT 组件?
要将 WinRT 二进制文件加载到 PowerShell,请使用此示例:
> [Windows.Data.Json.JsonValue,Windows.Web,ContentType=WindowsRuntime]
> $value = new-object Windows.Data.Json.JsonObject
> $value.Stringify()
{}
我想你需要 StorageFile
:
> [Windows.System.Launcher,Windows.Web,ContentType=WindowsRuntime]
> [Windows.System.Launcher]::LaunchFileAsync
OverloadDefinitions
-------------------
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file)
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file, Windows.System.LauncherOptions options)
要创建一个,您可以这样做:
> [Windows.Management.Core.ApplicationDataManager,Windows.Web,ContentType=WindowsRuntime]
> $value = [Windows.Management.Core.ApplicationDataManager]::CreateForPackageFamily("BackgroundTaskApp_mxmz85hp10cp4")
> $asyncInfo = $value.LocalFolder.CreateFileAsync("foo.txt")
对于 await *Async()
方法,您将需要执行类似 create a wrapper.
的操作
我正在尝试在 PowerShell 中使用 Windows.System
命名空间,它是 WinRT 的一部分。
程序集不是 dll
,而是 winmd
。两种加载程序集到PowerShell的方法好像都不行
#[System.Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Windows Kits.0\References\CommonConfiguration\Neutral\Windows.winmd")
#Add-Type -Path "C:\Program Files (x86)\Windows Kits.0\References\CommonConfiguration\Neutral\Windows.winmd"
我知道使用 Windows API 比加载我的 .NET 代码要复杂一些。我有 pinched 使用部分 win32 的代码,WinRT 解决方案是否类似?
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature)
{
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods()
{
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
$nativeMethodsCode
}
"@
}
Register-NativeMethod "user32.dll" "int MessageBox (IntPtr hWnd, string text, string caption,int type)"
Add-NativeMethods
[NativeMethods]::MessageBox(0, "Please do not press this again.", "Attention", 0)| Out-Null
我的目标是在 PowerShell 中 运行 Windows.System.Launcher.LaunchFileAsync("C:\file");
。如何加载我需要的 WinRT 组件?
要将 WinRT 二进制文件加载到 PowerShell,请使用此示例:
> [Windows.Data.Json.JsonValue,Windows.Web,ContentType=WindowsRuntime]
> $value = new-object Windows.Data.Json.JsonObject
> $value.Stringify()
{}
我想你需要 StorageFile
:
> [Windows.System.Launcher,Windows.Web,ContentType=WindowsRuntime]
> [Windows.System.Launcher]::LaunchFileAsync
OverloadDefinitions
-------------------
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file)
static Windows.Foundation.IAsyncOperation[bool] LaunchFileAsync(Windows.Storage.IStorageFile file, Windows.System.LauncherOptions options)
要创建一个,您可以这样做:
> [Windows.Management.Core.ApplicationDataManager,Windows.Web,ContentType=WindowsRuntime]
> $value = [Windows.Management.Core.ApplicationDataManager]::CreateForPackageFamily("BackgroundTaskApp_mxmz85hp10cp4")
> $asyncInfo = $value.LocalFolder.CreateFileAsync("foo.txt")
对于 await *Async()
方法,您将需要执行类似 create a wrapper.