C# 使用进程触发“打开为”对话框
为了好玩,我正在制作自己的文件浏览器。我无法使用“打开方式”或“打开方式”对话框打开文件,因为它不会出现。我找到了一个,但它只使用已知的程序文件路径(VLC,请参阅本文底部的代码)打开。但是我只想显示“打开为”对话框,以便用户可以自由选择使用该文件运行哪个程序。 这是我的密码C# 使用进程触发“打开为”对话框,c#,C#,为了好玩,我正在制作自己的文件浏览器。我无法使用“打开方式”或“打开方式”对话框打开文件,因为它不会出现。我找到了一个,但它只使用已知的程序文件路径(VLC,请参阅本文底部的代码)打开。但是我只想显示“打开为”对话框,以便用户可以自由选择使用该文件运行哪个程序。 这是我的密码 ProcessStartInfo startInfo = new ProcessStartInfo() { WindowStyle = ProcessWindowStyle.Normal, FileName
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
为了将来的参考,这是我在链接中提到的代码,上面的链接使用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)
编辑1并回答: 我已经做了上述“重复”问题中的代码,但出于某种原因,对话框没有提示。我决定检查文件路径,但有一个重复的斜杠,不允许出现提示/对话框 我以某种方式将左边的文件路径传递到进程中。我应该传递的正确值是右边的值。
C:\Documents\\file.exe
->C:\Documents\file.exe
为了大家的利益,我用这两行代码将“openas”/“openwith”对话框称为
string filePath = "C:\Documents\file.exe";
Process.Start("rundll32.exe", "shell32.dll, OpenAs_RunDLL " + filePath);
如果查找rundll32.exe
和shell32.dll
时出现问题,只需使用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();
另请参见发布到问题。更新了解决方案和实际问题请参见发布到问题。更新了解决方案和实际问题