Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A very primitive check would be to check for the file extension:</p> <pre><code>Path.GetExtension(filename).Equals(".exe", StringComparison.InvariantCultureIgnoreCase); </code></pre> <p>However, Windows supports a variety of extensions for executable files (e.g. .cmd, .com, .cpl, .scr and <a href="http://antivirus.about.com/od/securitytips/a/fileextview.htm" rel="nofollow noreferrer">many more</a>), so the check above would not cover all executable files.</p> <p>As mentioned by others, you can also check the magic numbers in the file header for the existence of e.g. MZ (and some other more rare signatures). This second check could be used in addition to checking the extension(s) though you can never be certain that the file is not a simple text file accidentally starting with the same text.</p> <p>If you are going to start the executable to be checked anyway, then it is probably safest to simply start it with proper exception handling:</p> <pre><code>const int ERROR_BAD_EXE_FORMAT = 193; try { ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = false; psi.FileName = @"C:\tmp\testOLElocal_4.docx"; Process.Start(psi); } catch (Win32Exception ex) { if (ex.NativeErrorCode == ERROR_BAD_EXE_FORMAT) { // The exception message would be // "The specified executable is not a valid application for this OS platform." // Console.WriteLine("Not a valid executable."); } else { throw; } } </code></pre> <p>NB: You didn't mention any details about your own application, but whenever executing code which comes via user input you should make sure that your user can be trusted.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload