Laden...

Prüfen, ob eine Datei ausfürbar ist (.exe, .bat, etc.)

Erstellt von Xqgene vor 16 Jahren Letzter Beitrag vor 16 Jahren 3.297 Views
X
Xqgene Themenstarter:in
2.051 Beiträge seit 2004
vor 16 Jahren
Prüfen, ob eine Datei ausfürbar ist (.exe, .bat, etc.)

Beschreibung:
thja... s. den Titel 😃

    [StructLayout(LayoutKind.Sequential)]
    internal struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    }

        [DllImport("shell32.dll")]
        public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes,
            ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);

        /// <summary>
        /// Prüft, ob eine Datei ausfürbar ist (.exe, .bat, etc.)
        /// </summary>
        /// <param name="path">Pfad zu der Datei.</param>
        /// <returns>true, wenn die Datei ausführbar ist, ansonsten false.</returns>
        public static bool IsExecutable(string path)
        {
            SHFILEINFO fi = new SHFILEINFO();
            public const int SHGFI_EXETYPE = 0x000002000;     // return exe type

            IntPtr res = SHGetFileInfo(
                path,
                0,
                ref fi,
                (uint)System.Runtime.InteropServices.Marshal.SizeOf(fi),
                SHGFI_EXETYPE);

            return (res != IntPtr.Zero);
        }

Schlagwörter: exe, ausführbar, Datei, prüfen, File, executable, check

Quelle: .NET-Snippets