我如何启动在C#中的文件 [英] How do i launch files in C#

查看:113
本文介绍了我如何启动在C#中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-Edit-我觉得自己像个白痴。我有一种感觉有点像回答以下会工作,但没有看到任何谷歌的结果类似下面的答案。所以当我看到这个复杂codeI认为它必须是这个样子。

我搜查,发现该<一href="http://stackoverflow.com/questions/24954/windows-list-and-launch-applications-associated-with-an-extension">Windows:与扩展但是它并没有回答我的问题有关的名单和启动应用程序。随着调整下面我想出了下面。然而,它被卡住的图像文件。 TXT文件运行正常

我会更新这个code很快考虑与不过我不明白为什么图像文件不推出的空间应用程序的路径。

 静态无效launchFile(字符串FN)
{
    //大部分是取自
    //http://stackoverflow.com/questions/24954/windows-list-and-launch-applications-associated-with-an-extension
    常量字符串extPathTemplate = @HKEY_CLASSES_ROOT \ {0};
    常量字符串cmdPathTemplate = @HKEY_CLASSES_ROOT \ {0} \壳\开放\命令;

    字符串EXT = Path.GetExtension(FN);

    VAR extPath =的String.Format(extPathTemplate,分机);

    可采用DocName VAR = Registry.GetValue(extPath,的String.Empty,的String.Empty)的字符串;
    如果(!string.IsNullOrEmpty(可采用DocName))
    {
        // 2.找出哪些命令与我们的扩展相关
        VAR associatedCmdPath =的String.Format(cmdPathTemplate,可采用DocName);
        VAR associatedCmd = Registry.GetValue(associatedCmdPath,的String.Empty,的String.Empty)的字符串;

        如果(!string.IsNullOrEmpty(associatedCmd))
        {
            //Console.WriteLine(\{0} \命令与{1}扩展关联,associatedCmd,EXT);
            VAR P =新的Process();
            p.StartInfo.FileName = associatedCmd.Split('')[0];
            字符串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();
        }
    }
}
 

解决方案

为什么不试试:

System.Diagnostics.Process.Start(文件路径);

这将使用将被打开,如果你只是点击它的默认程序。诚然它不会让你选择要运行的程序......但假设你想模仿,将被使用,如果用户要在文件上双击的行为。这应该只是罚款

-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 extension however 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

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

解决方案

Why not just try:

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.

这篇关于我如何启动在C#中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆