如何在 C# 中启动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1283584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I launch files in C#
提问by
-Edit- I feel like an idiot. I had a feeling something like the answer below would work but didnt see any google results similar to the answers below. So when i saw this complex code i thought it had to be this way.
-编辑-我觉得自己像个白痴。我有一种感觉,类似于下面的答案会起作用,但没有看到任何类似于下面的答案的谷歌结果。所以当我看到这个复杂的代码时,我认为它必须是这样的。
I searched and found this Windows: List and Launch applications associated with an extensionhowever it didnt answer my question. With tweaks below i came up with the below. However it gets stuck on image files. Txt files run fine
我搜索并找到了这个Windows: List and Launch applications associated with an extension但是它没有回答我的问题。通过下面的调整,我想出了下面的内容。但是它会卡在图像文件上。txt 文件运行良好
I will update this code soon to account for app paths with spaces however i dont understand why image files dont launch.
我将很快更新此代码以说明带有空格的应用程序路径,但是我不明白为什么图像文件不启动。
static void launchFile(string fn)
{
//majority was taken from
//https://stackoverflow.com/questions/24954/windows-list-and-launch-applications-associated-with-an-extension
const string extPathTemplate = @"HKEY_CLASSES_ROOT\{0}";
const string cmdPathTemplate = @"HKEY_CLASSES_ROOT\{0}\shell\open\command";
string ext = Path.GetExtension(fn);
var extPath = string.Format(extPathTemplate, ext);
var docName = Registry.GetValue(extPath, string.Empty, string.Empty) as string;
if (!string.IsNullOrEmpty(docName))
{
// 2. Find out which command is associated with our extension
var associatedCmdPath = string.Format(cmdPathTemplate, docName);
var associatedCmd = Registry.GetValue(associatedCmdPath, string.Empty, string.Empty) as string;
if (!string.IsNullOrEmpty(associatedCmd))
{
//Console.WriteLine("\"{0}\" command is associated with {1} extension", associatedCmd, ext);
var p = new Process();
p.StartInfo.FileName = associatedCmd.Split(' ')[0];
string s2 = associatedCmd.Substring(p.StartInfo.FileName.Length + 1);
s2 = s2.Replace("%1", string.Format("\"{0}\"", fn));
p.StartInfo.Arguments = s2;//string.Format("\"{0}\"", fn);
p.Start();
}
}
}
回答by Steven Evers
Use:
用:
System.Diagnostics.Process.Start(filePath);
It will use the default program that would be opened as if you just clicked on it. Admittedly it doesn't let you choose the program that will run... but assuming that you want to mimic the behaviour that would be used if the user were to double-click on the file, this should work just fine.
它将使用默认程序,就像您刚刚单击它一样打开。诚然,它不会让您选择将要运行的程序……但假设您想模仿用户双击文件时将使用的行为,这应该可以正常工作。
回答by Matthew Iselin
It really sounds like you're looking more for this:
听起来您确实在寻找更多内容:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "<whatever>";
proc.Start();
回答by Ganesh R.
Assuming that you just want to launch files which already have some associated applications (eg: *.txt is associated with notepad), Use System.Diagnostics.Process.
假设您只想启动已经有一些关联应用程序的文件(例如:*.txt 与记事本关联),请使用 System.Diagnostics.Process。
e.g. :
例如:
using System.Diagnostics;
Process p = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.UseShellExecute = true;
pi.FileName = @"MY_FILE_WITH_FULL_PATH.jpg";
p.StartInfo = pi;
try
{
p.Start();
}
catch (Exception Ex)
{
//MessageBox.Show(Ex.Message);
}
Note: In my PC, the pic opens in Windows Picture & Fax Viewer since that is the default application for *.jpg files.
注意:在我的 PC 中,图片在 Windows 图片和传真查看器中打开,因为这是 *.jpg 文件的默认应用程序。