Results 1 to 6 of 6

Thread: Double click an image in Windows Explorer into my application.

  1. #1

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Question Double click an image in Windows Explorer into my application.

    I'm in the middle of a simple image viewer that I'm writing for my mother. I've pretty much finished except for 1 minor detail.

    I simply want my application to be the default image viewer. I've searched high and low all over the internet and can't find a suitable answer

    I know how to manually change the registry which I've already done. Changing the registry programmatically can come later, but the application opens when I double a click an image.

    The problem is... My code obviously doesn't contain a handle for it. How I create this handle is unknown.

    Any help will be greatly appreciated.


    ATTENTION SEARCHERS
    This method works perfectly.
    Code:
     
        Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim sFileName As String
            Dim args() As [String] = Environment.GetCommandLineArgs()
            sFileName = args(1)
            MsgBox(sFileName)
        End Sub
    Last edited by Grags; Jun 21st, 2017 at 08:06 PM.
    The glass is half full if you're filling it, and half empty if you're emptying it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Double click an image in Windows Explorer into my application.

    When your application is opened by Windows in response to double-clicking a file, the file path is passed to your application as a commandline argument. There are a number of ways to access the commandline arguments for the current process but probably the most common in Windows Forms is to call Environment.GetCommandlineArgs in the Load event handler of the startup form. You'll find plenty of examples about this site and elsewhere with a search. Once you have the file path, you use it like any other file path, so it's up to you to decide how to use it in your app. Also, keep in mind that anyone could run your app from a command prompt and pass any argument at all, so write your code in a way that invalid arguments won't cause it to crash.

  3. #3

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Double click an image in Windows Explorer into my application.

    Quote Originally Posted by jmcilhinney View Post
    When your application is opened by Windows in response to double-clicking a file, the file path is passed to your application as a commandline argument. There are a number of ways to access the commandline arguments for the current process but probably the most common in Windows Forms is to call Environment.GetCommandlineArgs in the Load event handler of the startup form. You'll find plenty of examples about this site and elsewhere with a search. Once you have the file path, you use it like any other file path, so it's up to you to decide how to use it in your app. Also, keep in mind that anyone could run your app from a command prompt and pass any argument at all, so write your code in a way that invalid arguments won't cause it to crash.
    Yup, this is pretty much why I created this thread. Because I've spent hours reading the same thing all over the internet. Stuff I don't understand. I just prayed if I worded it different to the millions of threads I've been reading all night someone would say actually grags. All you need to do is...

    Handler
    Load image
    end handler.


    There you go pal. Have a nice day!
    The glass is half full if you're filling it, and half empty if you're emptying it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Double click an image in Windows Explorer into my application.

    I'm not sure whether you're saying that I have provided that or not.

  5. #5

    Thread Starter
    Hyperactive Member Grags's Avatar
    Join Date
    Apr 2014
    Posts
    268

    Re: Double click an image in Windows Explorer into my application.

    Quote Originally Posted by jmcilhinney View Post
    I'm not sure whether you're saying that I have provided that or not.
    Using you answer. I was able to find a sample. I've edited the sample to make it extremely simple and straightforward for anyone else who comes across this problem.

    Code:
     
        Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
            Dim sFileName As String
            Dim args() As [String] = Environment.GetCommandLineArgs()
            sFileName = args(1)
            MsgBox(sFileName)
        End Sub

    All I need now is to change the registry programmatically. Though at this time it's not important enough for me to spend multiple more hours finding dead ends on Google. The application works as intended. It's done.

    Thanks for your help.
    Last edited by Grags; Jun 21st, 2017 at 08:18 PM.
    The glass is half full if you're filling it, and half empty if you're emptying it.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Double click an image in Windows Explorer into my application.

    That code could crash your app under certain circumstances or lead to other unexpected behaviour. It assumes that at least one commandline argument was passed to your app so will throw an ArgumentOutOfRangeException if you run the app without a commandline argument. On 32-bit machines, that will cause the app to crash. On 64-bit machines, the app will not crash but no code after that line will be executed.

    Registering your app as a handler for specific file types is something that the installer usually does, although you might provide functionality in the app to change it after installation too. If you publish your project to create a ClickOnce installer then you can do that via the project properties. Most other deployment tools will provide something similar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width