none
Get an application's path RRS feed

  • Question

  • Hey there, I've made a windows application form and then I set a certain file format, for instance, FILE_NAME.abc (the file format is .abc)
    to be opened by this application form, by simply double clicking the file with the custom format and selecting the windows application form as the default program.
    Now, I need to know the path of the file with the custom format that opened the application form.
    Does someone know how to do that?
    Thanks from advance.
    Tuesday, September 25, 2018 4:35 PM

Answers

  • Somewhere in your application source files you have Program.cs

    Modify the source to be like this:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

    and check values in the args array.

    Most likely first or second will be the file reference.


    Sincerely, Highly skilled coding monkey.

    • Marked as answer by dindibo4 Tuesday, September 25, 2018 7:33 PM
    Tuesday, September 25, 2018 5:35 PM

All replies

  • Not sure if I have understood...

    If you want to get the executable associated with an extension, it is done with AssocQueryString 

    For example, test with .bmp :

    {
        string sExtension = ".bmp";
        int nLength = 260;
        var sbAssociatedExe = new StringBuilder(nLength);
        HRESULT hr = AssocQueryString(AssocF.ASSOCF_NONE, AssocStr.ASSOCSTR_EXECUTABLE, sExtension, null, sbAssociatedExe, ref nLength);
    }

    Declarations :

    public enum HRESULT : int
    {
        S_OK = 0,
        S_FALSE = 1,
        E_NOINTERFACE = unchecked((int)0x80004002),
        E_NOTIMPL = unchecked((int)0x80004001),
        E_FAIL = unchecked((int)0x80004005)
    }
    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 HRESULT AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, ref int pcchOut);

    Tuesday, September 25, 2018 4:48 PM
  • I'm not exactly sure what this code does, I've tried it on my project but  I didn't see any paths saved in the variables.

    What I mean is that I have a file that called "test.bitmap" and inside the file there is data that contains an image.

    I've written a method that takes a file path, that ends with .bitmap, and it decodes it to System.Drawing.Bitmap .

    My goal is to be able to double click on a file that ends with .bitmap and the windows application will be automatically launched and it will decode the file data and will show the decoded image as a bitmap, this can be done by passing to the method that decodes the .bitmap file to System.Drawing.Bitmap the file's path and it will do the rest, but the problem is that I don't know how to get the path of the file that opened the form, in this case, the path of "test.bitmap" file.

    I hope that I made myself clear, thanks for the reply.

    ps. I already know how to launch the application when I double click the .bitmap file but as I've mentioned, I need to get the path of the file that called the application
    • Edited by dindibo4 Tuesday, September 25, 2018 5:14 PM
    Tuesday, September 25, 2018 5:12 PM
  • It is likely that when you established the file association between the .bitmap extension and your application that Windows set it up so that the path information for the .bitmap file that is opened is passed to your application as a command line argument.

    • Edited by RLWA32 Tuesday, September 25, 2018 5:23 PM
    Tuesday, September 25, 2018 5:21 PM
  • Somewhere in your application source files you have Program.cs

    Modify the source to be like this:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

    and check values in the args array.

    Most likely first or second will be the file reference.


    Sincerely, Highly skilled coding monkey.

    • Marked as answer by dindibo4 Tuesday, September 25, 2018 7:33 PM
    Tuesday, September 25, 2018 5:35 PM
  • Somewhere in your application source files you have Program.cs

    Modify the source to be like this:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

    and check values in the args array.

    Most likely first or second will be the file reference.


    Sincerely, Highly skilled coding monkey.

    I've modified it to be exactly like that, but the args string array was empty(the array's length was 0).

     
    Tuesday, September 25, 2018 6:23 PM
  • If you run it from Studio - you need to specify arguments - full file name, quoted - in project Propertes => Debug => Command Line Argument.

    If it happen in Win environment - most likely you have wrong association - may be didn't specify (it's in registry) that there can be a parameter.


    Sincerely, Highly skilled coding monkey.

    Tuesday, September 25, 2018 6:37 PM
  • Somewhere in your application source files you have Program.cs

    Modify the source to be like this:

        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

    and check values in the args array.

    Most likely first or second will be the file reference.


    Sincerely, Highly skilled coding monkey.

    I'm sorry, my bad.

    This method work, I just forgot to run the application through the .bitmap file and I accidentally ran it through visual studio.

    Thanks for the help. 

    Tuesday, September 25, 2018 7:34 PM