PowerShell实现查询打开某个文件的默认应用程序(powershell 获取文件大小)太疯狂了

随心笔谈3年前发布 admin
189 0 0

文章摘要

这篇文章介绍了一段C#代码,用于调用外部API(API通常指应用程序编程接口)。代码中定义了一个名为`Win32API`的类,其中包含一个静态方法`FindExecutableA`,用于从`shell32.dll`库中调用`FindExecutable`函数。该函数接受文件路径和结果缓冲区作为参数,并返回一个`long`值表示调用结果。`FindExecutable`方法调用`FindExecutableA`,根据返回值处理结果:如果结果大于等于32,则返回结果字符串;否则返回错误信息。最后,代码中使用该方法调用`FindExecutable`,并将返回值存储在`Executable`变量中,并打印出文件路径和执行结果。


$Source=@”

using System;
using System.Text;
using System.Runtime.InteropServices;
public class Win32API
{
[DllImport(“shell32.dll”, EntryPoint=”FindExecutable”)]

public static extern long FindExecutableA(string lpFile, string lpDirectory, StringBuilder lpResult);

public static string FindExecutable(string pv_strFilename)
{
StringBuilder objResultBuffer=new StringBuilder(1024);
long lngResult=0;

lngResult=FindExecutableA(pv_strFilename, string.Empty, objResultBuffer);

if(lngResult >=32)
{
return objResultBuffer.ToString();
}

return string.Format(“Error: ({0})”, lngResult);
}
}

“@

Add-Type -TypeDefinition $Source -ErrorAction SilentlyContinue

$FullName=’c:\Windows\windowsupdate.log’
$Executable=[Win32API]::FindExecutable($FullName)

“$FullName will be launched by $Executable”

© 版权声明

相关文章