3

I'm making my own File Explorer for fun. I have trouble opening a file with the "Open with" or "Open as" dialog as it won't appear. I found a link but it only makes use of a known program file path (VLC, see code at the bottom of this post) to open with. But I just want the "Open As" dialog appear so that the user can freely select which program to run with the file.

This is my code.

ProcessStartInfo startInfo = new ProcessStartInfo()
{
    WindowStyle = ProcessWindowStyle.Normal,
    FileName = @"D:\file.exe",// the file I wanted to "open as" with
    Verb = "openas",
    UseShellExecute = true,
    ErrorDialog = true
};
try { Process.Start(startInfo); }
catch (Win32Exception) { }//catches when user cancelled the action

For future reference, this is the code that I was referring to in the link above which opens any file using VLC:

var path = files[currentIndex].fileName;
var pi = new ProcessStartInfo(path)
{
    Arguments = Path.GetFileName(path),
    UseShellExecute = true,
    WorkingDirectory = Path.GetDirectoryName(path),
    FileName = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe",
    Verb = "OPEN"
};
Process.Start(pi)

Edit 1 & Answer:

I already did the code in the said "duplicate" questions but for some reason the dialog was not prompting. I decided to check the file path and there was a duplicate slash disallowing the prompt/dialog from appearing.

I somehow passed the left file path into the process. The correct value that I should have passed was the one on the right. C:\Documents\\file.exe -> C:\Documents\file.exe

And for the sake of everyone, I used this two liner to call the "Open as"/"Open with" dialog

string filePath = "C:\Documents\file.exe";
Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + filePath);

And if there's a problem finding the rundll32.exe and the shell32.dll, simply use the ff:

Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "rundll32.exe");
proc.StartInfo.Arguments = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll") + ",OpenAs_RunDLL " + filePath;
proc.Start();
CraftedGaming
  • 499
  • 7
  • 21

0 Answers0