简体   繁体   中英

Open With Dialogue needed from DataGridView C#

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!!!

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#

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);
    }
  }
}

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM