CNC中我觉得自己像个白痴。 我有以下感觉像答案的工作,但没有看到任何谷歌的结果类似下面的答案。 所以当我看到这个复杂的代码,我想这已经是这个样子。
我搜索,发现这个Windows系统:带有扩展名关联列表和启动应用程序但是它没有回答我的问题。 随着调整下面我想出了下面。 然而,它卡住上的图像文件。 txt文件运行正常
我会尽快更新这个代码,以解决与空间,但是我不明白为什么图像文件不启动应用程序的路径。
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();
}
}
}