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

随心笔谈9个月前发布 admin
181 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

该代码通过反射调用外部DLL(`shell32.dll`)中的`FindExecutable`方法,用于查找指定文件或目录的路径。主要功能包括: 1. 使用`Win32API`类中的静态方法`FindExecutableA`进行外部调用。2. 通过`StringBuilder`收集调用结果,并根据返回值判断查找成功与否。3. 在成功情况下返回文件路径,否则返回错误信息(`Error: (32)`等)。 代码还定义了类型定义,用于在环境中运行脚本文件(如`windowupdate.log`),并连接文件名称与可执行文件路径。整体功能是通过反射机制实现对系统文件路径的自动化查找与验证。


$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”

© 版权声明

相关文章