首页 / C# / c# – 如何确定文件是否可执行?
c# – 如何确定文件是否可执行?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何确定文件是否可执行?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5145字,纯文字阅读大概需要8分钟。
内容图文
![c# – 如何确定文件是否可执行?](/upload/InfoBanner/zyjiaocheng/760/d27f3f7edbdf47b1b9cd844a61ef60cd.jpg)
我正在编写一个程序((部分工作)执行其他程序.我希望能够使用Process.Start运行尽可能多的类型的程序(用不同的语言编写).所以,我想我应该:
>打开文件
>阅读第一行
>检查它是否以#开头!
>如果是这样,请使用#后面的内容!作为要执行的程序,并将文件名作为参数传递
>如果没有#!找到,检查文件扩展名对已知程序的字典(例如.py – > python)并执行该程序
>否则,只需尝试执行该文件并捕获任何错误
但是,我认为实际检查文件是否可执行是否更容易/更有效,如果是,请跳转到6.有没有办法做到这一点?
解决方法:
执行此操作的唯一方法是使用P / Invoke调用Win32 API.您需要使用SHGetFileInfo方法,然后解压返回值:
[Flags]
internal enum SHGFI : uint
{
ADDOVERLAYS = 0x20,
ATTR_SPECIFIED = 0x20000,
ATTRIBUTES = 0x800,
DISPLAYNAME = 0x200,
EXETYPE = 0x2000,
ICON = 0x100,
ICONLOCATION = 0x1000,
LARGEICON = 0,
LINKOVERLAY = 0x8000,
OPENICON = 2,
OVERLAYINDEX = 0x40,
PIDL = 8,
SELECTED = 0x10000,
SHELLICONSIZE = 4,
SMALLICON = 1,
SYSICONINDEX = 0x4000,
TYPENAME = 0x400,
USEFILEATTRIBUTES = 0x10
}
/// <summary>
/// This structure contains information about a file object.
/// </summary>
/// <remarks>
/// This structure is used with the SHGetFileInfo function.
/// </remarks>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct SHFILEINFO
{
/// <summary>
/// Handle to the icon that represents the file.
/// </summary>
internal IntPtr hIcon;
/// <summary>
/// Index of the icon image within the system image list.
/// </summary>
internal int iIcon;
/// <summary>
/// Specifies the attributes of the file object.
/// </summary>
internal SFGAO dwAttributes;
/// <summary>
/// Null-terminated string that contains the name of the file as it
/// appears in the Windows shell, or the path and name of the file that
/// contains the icon representing the file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = Constants.MAX_PATH)]
internal string szDisplayName;
/// <summary>
/// Null-terminated string that describes the type of file.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
internal string szTypeName;
}
/// <summary>
/// Specifies the executable file type.
/// </summary>
public enum ExecutableType : int
{
/// <summary>
/// The file executable type is not able to be determined.
/// </summary>
Unknown = 0,
/// <summary>
/// The file is an MS-DOS .exe, .com, or .bat file.
/// </summary>
DOS,
/// <summary>
/// The file is a Microsoft Win32?-based console application.
/// </summary>
Win32Console,
/// <summary>
/// The file is a Windows application.
/// </summary>
Windows,
}
// Retrieves information about an object in the file system,
// such as a file, a folder, a directory, or a drive root.
[DllImport("shell32",
EntryPoint = "SHGetFileInfo",
ExactSpelling = false,
CharSet = CharSet.Auto,
SetLastError = true)]
internal static extern IntPtr SHGetFileInfo(
string pszPath,
FileAttributes dwFileAttributes,
ref SHFILEINFO sfi,
int cbFileInfo,
SHGFI uFlags);
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
private ExecutableType IsExecutable(string fileName)
{
if (fileName == null)
{
throw new ArgumentNullException("fileName");
}
ExecutableType executableType = ExecutableType.Unknown;
if (File.Exists(fileName)
{
// Try to fill the same SHFILEINFO struct for the exe type. The returned pointer contains the encoded
// executable type data.
ptr = IntPtr.Zero;
ptr = SHGetFileInfo(fileName, FileAttributes.Normal, ref this.shellFileInfo, Marshal.SizeOf(typeof(SHFILEINFO)), SHGFI.EXETYPE);
// We need to split the returned pointer up into the high and low order words. These are important
// because they help distinguish some of the types. The possible values are:
//
// Value Meaning
// ----------------------------------------------------------------------------------------------
// 0 Nonexecutable file or an error condition.
// LOWORD = NE or PE and HIWORD = Windows version Microsoft Windows application.
// LOWORD = MZ and HIWORD = 0 Windows 95, Windows 98: Microsoft MS-DOS .exe, .com, or .bat file
// Microsoft Windows NT, Windows 2000, Windows XP: MS-DOS .exe or .com file
// LOWORD = PE and HIWORD = 0 Windows 95, Windows 98: Microsoft Win32 console application
// Windows NT, Windows 2000, Windows XP: Win32 console application or .bat file
// MZ = 0x5A4D - DOS signature.
// NE = 0x454E - OS/2 signature.
// LE = 0x454C - OS/2 LE or VXD signature.
// PE = 0x4550 - Win32/NT signature.
int wparam = ptr.ToInt32();
int loWord = wparam & 0xffff;
int hiWord = wparam >> 16;
if (wparam == 0)
{
executableType = ExecutableType.Unknown;
}
else
{
if (hiWord == 0x0000)
{
if (loWord == 0x5A4D)
{
// The file is an MS-DOS .exe, .com, or .bat
executableType = ExecutableType.DOS;
}
else if (loWord == 0x4550)
{
executableType = ExecutableType.Win32Console;
}
}
else
{
if (loWord == 0x454E || loWord == 0x4550)
{
executableType = ExecutableType.Windows;
}
else if (loWord == 0x454C)
{
executableType = ExecutableType.Windows;
}
}
}
}
return executableType;
}
(这应该可行,但是从较大的库中提取,因此可能存在小问题.但是,它应该足够完整,以便在那里获得大部分内容.)
内容总结
以上是互联网集市为您收集整理的c# – 如何确定文件是否可执行?全部内容,希望文章能够帮你解决c# – 如何确定文件是否可执行?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。