2

I am developing an application and I need to read the EXIF details of JPG file. I am able to do so with an OpenFileDialog button but, I want it to be so that a user can open a JPG file with my application (right click>Open with) and I could get the path of the JPG file in string.

I just want to know how to get the path of the file on which the user right clicked ad selected open with "My Application"

prmottajr
  • 1,816
  • 1
  • 13
  • 22
Haider Ali Punjabi
  • 345
  • 1
  • 3
  • 13

2 Answers2

1

If you know how to get the EXIF data already and just want users to be able to right-click a JPG file > Open with > Select your application and then get the filename they're trying to open, you can do this:

private void testButton_Click(object sender, EventArgs e)
{
    string[] cmdLineArgs = Environment.GetCommandLineArgs();
    string jpgFilenameToOpen = "None";

    if (cmdLineArgs.Length > 1)
    {
       jpgFilenameToOpen = cmdLineArgs[1];
    }

    YourGetEXIFDetailsMethod(jpgFilenameToOpen);
}

The Environment.GetCommandLineArgs() returns an array with all command line arguments that was passed to your application on load. Normally, if they just pass a filename, it should be the 2nd item in the array.

You can also loop through the arguments if needed by doing this:

foreach (var arg in cmdLineArgs)
{
    MessageBox.Show(arg.ToString());
}

Edit:

I just realized I'm not sure if you need to accept only one JPG filename at a time or if you need to accept multiple JPG files at once. If it's the latter, here's some updated code that can loop through all the command-line arguments and do something with only JPG/JPEG files:

private void Form1_Load(object sender, EventArgs e)
{
    string[] cmdLineArgs = Environment.GetCommandLineArgs();
    List<string> jpgFilenamesToAnalyze = new List<string>();

    foreach (var arg in cmdLineArgs)
    {
        if (arg.Contains(".jpg") || arg.Contains(".jpeg"))
        {
            jpgFilenamesToAnalyze.Add(arg);
        }
    }

    if (jpgFilenamesToAnalyze.Count > 0)
    {
        StringBuilder sbJPGFiles = new StringBuilder();
        sbJPGFiles.AppendLine("Found " + jpgFilenamesToAnalyze.Count + " images to analyze:\n\nFiles:");

        foreach (var jpgFilename in jpgFilenamesToAnalyze)
        {
            // YourGetEXIFDataMethod(jpgFilename)
            sbJPGFiles.AppendLine(jpgFilename);
        }

        MessageBox.Show(sbJPGFiles.ToString());
    }
    else
    {
        MessageBox.Show("No images found to analyze");
    }
}
Calcolat
  • 878
  • 2
  • 11
  • 22
1

You need to register your application for the JPG file extension in the Registry as described here: https://msdn.microsoft.com/en-us/library/windows/desktop/cc144175(v=vs.85).aspx

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139