none
Known file extensions

    Question

  • Hello,

    How can i check if a filename has an extension for which the operating system running my program doesn't ask to select an app to open the file if i start it.

    Thank you very much

    Manuel

    Friday, April 21, 2017 8:54 AM

Answers

  • Here is an example of FindExecutable where I'm pointing to an existing file that is hard coded for this sample.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.IO;
    
    namespace Selections
    {
        public partial class Form1 : Form
        {
    
            [DllImport("shell32.dll")]
            static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult);
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                var fullName = @"C:\Users\Karens\Documents\script.sql";
                var name = Path.GetFileName(fullName);
                var path = Path.GetDirectoryName(fullName);
                StringBuilder sb = new StringBuilder();
                var test = FindExecutable(name, path, sb);
                if (test == 2 || test == 31)
                {
                    MessageBox.Show($"There is no application associated with the given file name extension {Environment.NewLine}'{Path.GetExtension(name)}'.");
                }
                else
                {
                    MessageBox.Show($"Executable {sb}");
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Manuel Marini Friday, April 21, 2017 3:09 PM
    Friday, April 21, 2017 1:21 PM
    Moderator

All replies

  • You can use AssocQueryString

    (with AssocStr.ASSOCSTR_EXECUTABLE for Win32 applications and AssocStr.ASSOCSTR_DELEGATEEXECUTE in case of UWP applications)

            [Flags]
            enum AssocF : uint
            {
                ASSOCF_NONE = 0,
                ASSOCF_INIT_NOREMAPCLSID = 0x1,
                ASSOCF_INIT_BYEXENAME = 0x2,
                ASSOCF_OPEN_BYEXENAME = 0x2,
                ASSOCF_INIT_DEFAULTTOSTAR = 0x4,
                ASSOCF_INIT_DEFAULTTOFOLDER = 0x8,
                ASSOCF_NOUSERSETTINGS = 0x10,
                ASSOCF_NOTRUNCATE = 0x20,
                ASSOCF_VERIFY = 0x40,
                ASSOCF_REMAPRUNDLL = 0x80,
                ASSOCF_NOFIXUPS = 0x100,
                ASSOCF_IGNOREBASECLASS = 0x200,
                ASSOCF_INIT_IGNOREUNKNOWN = 0x400,
                ASSOCF_INIT_FIXED_PROGID = 0x800,
                ASSOCF_IS_PROTOCOL = 0x1000,
                ASSOCF_INIT_FOR_FILE = 0x2000
            }
    
            enum AssocStr
            {
                ASSOCSTR_COMMAND = 1,
                ASSOCSTR_EXECUTABLE,
                ASSOCSTR_FRIENDLYDOCNAME,
                ASSOCSTR_FRIENDLYAPPNAME,
                ASSOCSTR_NOOPEN,
                ASSOCSTR_SHELLNEWVALUE,
                ASSOCSTR_DDECOMMAND,
                ASSOCSTR_DDEIFEXEC,
                ASSOCSTR_DDEAPPLICATION,
                ASSOCSTR_DDETOPIC,
                ASSOCSTR_INFOTIP,
                ASSOCSTR_QUICKTIP,
                ASSOCSTR_TILEINFO,
                ASSOCSTR_CONTENTTYPE,
                ASSOCSTR_DEFAULTICON,
                ASSOCSTR_SHELLEXTENSION,
                ASSOCSTR_DROPTARGET,
                ASSOCSTR_DELEGATEEXECUTE,
                ASSOCSTR_SUPPORTED_URI_PROTOCOLS,
                ASSOCSTR_PROGID,
                ASSOCSTR_APPID,
                ASSOCSTR_APPPUBLISHER,
                ASSOCSTR_APPICONREFERENCE,
                ASSOCSTR_MAX
            }
    
            [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref int pcchOut);


    • Edited by Castorix31 Friday, April 21, 2017 12:25 PM
    Friday, April 21, 2017 9:24 AM
  • Hi Manuel Marini,

    Well there is a option in C# to call open With File Dialog. You have to use the "shell32.dll". Please look into below articles. Hope this helps you.

    How to prompt the “Choose a default program” in code?

    How can I show the “Open with” file dialog?

    Calling the Open With dialog box from your application, using C#



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Friday, April 21, 2017 12:14 PM
    Moderator
  • If you have an actual filename then I would use the FindExecutable API function call to see if there is an associated application:

    http://www.pinvoke.net/default.aspx/shell32.findexecutable


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, April 21, 2017 12:54 PM
  • If you have an actual filename then I would use the FindExecutable API function call to see if there is an associated application:

    http://www.pinvoke.net/default.aspx/shell32.findexecutable


    Paul ~~~~ Microsoft MVP (Visual Basic)

    The problem is that FindExecutable doesn't work with UWP applications

    (at least on my OS, Windows 10 14393.953)

    Friday, April 21, 2017 1:20 PM
  • Here is an example of FindExecutable where I'm pointing to an existing file that is hard coded for this sample.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.IO;
    
    namespace Selections
    {
        public partial class Form1 : Form
        {
    
            [DllImport("shell32.dll")]
            static extern int FindExecutable(string lpFile, string lpDirectory, [Out] StringBuilder lpResult);
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                var fullName = @"C:\Users\Karens\Documents\script.sql";
                var name = Path.GetFileName(fullName);
                var path = Path.GetDirectoryName(fullName);
                StringBuilder sb = new StringBuilder();
                var test = FindExecutable(name, path, sb);
                if (test == 2 || test == 31)
                {
                    MessageBox.Show($"There is no application associated with the given file name extension {Environment.NewLine}'{Path.GetExtension(name)}'.");
                }
                else
                {
                    MessageBox.Show($"Executable {sb}");
                }
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Manuel Marini Friday, April 21, 2017 3:09 PM
    Friday, April 21, 2017 1:21 PM
    Moderator
  • If you have an actual filename then I would use the FindExecutable API function call to see if there is an associated application:

    http://www.pinvoke.net/default.aspx/shell32.findexecutable


    Paul ~~~~ Microsoft MVP (Visual Basic)

    The problem is that FindExecutable doesn't work with UWP applications

    (at least on my OS, Windows 10 14393.953)

    I wasn't aware the OP was developing a UWP application. I think that question would belong in another forum.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, April 21, 2017 1:32 PM
  • Thank you very much !!!
    Friday, April 21, 2017 3:10 PM
  • If you have an actual filename then I would use the FindExecutable API function call to see if there is an associated application:

    http://www.pinvoke.net/default.aspx/shell32.findexecutable


    Paul ~~~~ Microsoft MVP (Visual Basic)

    The problem is that FindExecutable doesn't work with UWP applications

    (at least on my OS, Windows 10 14393.953)

    I wasn't aware the OP was developing a UWP application. I think that question would belong in another forum.


    Paul ~~~~ Microsoft MVP (Visual Basic)


    No, I mean that  if the extension is associated with a UWP aplication, FindExecutable won't work.
    Friday, April 21, 2017 3:19 PM
  • Hmm...thought I had already posted a link to FindExecutable.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, April 21, 2017 4:02 PM