0

I have a Windows form in which I have a DataGridView that displays the list of files in a location. I have created a ContextMenu that will drop down on right click on a file name. Options in the menu are "Copy", "Open With" etc.

When I click "open With" in the menu I need to get the "Open With" dialogue box as in Windows and then I should be able to select the application with which I can open the file with.

I am not sure how to get the "Open With" Dialogue box. Can someone please help me with this?

Thanks in Advance!!!

Arun
  • 11
  • 2

3 Answers3

0

This is maybe what you are looking for, just change what needs to be changed to accommodate for your app.

Open file dialog and select a file using WPF controls and C#

Community
  • 1
  • 1
0

Try this:

using System.Diagnostics;
//...

private void openWith_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "application (*.exe)|*.exe" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

  if(openFileDialog1.ShowDialog() == DialogResult.OK)
  {
    try
    {
      ProcessStartInfo pi = new ProcessStartInfo();
      pi.Arguments = Path.GetFileName(file);//the file that you want to open
      pi.UseShellExecute = true;
      pi.WorkingDirectory = Path.GetDirectoryName(file);
      pi.FileName = openDialog1.FileName;
      pi.Verb = "OPEN";
      Process.Start(pi);

    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: Could not open the file with this program. Original error: " + ex.Message);
    }
  }
}
ocuenca
  • 38,548
  • 11
  • 89
  • 102
  • ,@Luke Holmwood -- Thanks for the response :) I think I have not explained my requirements clearly. Here is my requirement. 1) I have a windows form, where I can browse to and select a folder and hit search. That will bring up the list of all the files in that folder and show it on the form in a DataGridView. 2) I can open any of these files by double clicking the cell in DataGridView. The file will be opened in the windows default application for that file format. 3) If I right click on the cell, it will drop down a menu which has "Copy File Path", "Copy File" and "Open With". – Arun Dec 19 '14 at 09:18
  • 4) "Copy File Path" will copy the path of the file to the clipboard. "Copy File" will place the file in to clipboard. 5) "Open With" option, when clicked, should bring up the list of applications in that PC with which I can open the file (just like as we have in Windows OS right click->open with). I am done with all except the 5th step. Can you please check on it again. – Arun Dec 19 '14 at 09:19
0

You can use ShellExecuteEx function.
Usage of the example is OpenWith("Path to File");

        [Serializable]
    public struct ShellExecuteInfo
    {
        public int Size;
        public uint Mask;
        public IntPtr hwnd;
        public string Verb;
        public string File;
        public string Parameters;
        public string Directory;
        public uint Show;
        public IntPtr InstApp;
        public IntPtr IDList;
        public string Class;
        public IntPtr hkeyClass;
        public uint HotKey;
        public IntPtr Icon;
        public IntPtr Monitor;
    }

    // Code For OpenWithDialog Box
    [DllImport("shell32.dll", SetLastError = true)]
    extern public static bool
           ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);

    public const uint SW_NORMAL = 1;

    static void OpenWith(string file)
    {
        ShellExecuteInfo sei = new ShellExecuteInfo();
        sei.Size = Marshal.SizeOf(sei);
        sei.Verb = "openas";
        sei.File = file;
        sei.Show = SW_NORMAL;
        if (!ShellExecuteEx(ref sei))
            throw new System.ComponentModel.Win32Exception();
    }
Avram
  • 4,267
  • 33
  • 40