【问题标题】:Starting another application from within C# code从 C# 代码中启动另一个应用程序
【发布时间】:2012-01-03 07:06:30
【问题描述】:

如何从 C# 代码中启动另一个应用程序?我无法让这件作品正常工作

    System.Diagnostics.Process.Start(@"%userprofile%\AppData\Local\Google\Application\chrome.exe");

编辑: 哇,我很愚蠢,只是注意到我在文件路径中忘记了什么。感谢您的回答,尽管他们帮助教了我一些其他有用的东西。

【问题讨论】:

  • 问题出在哪里?
  • 您也可以从Environment 类中获取环境变量的值。
  • 如果 Chrome 没有安装,或者没有安装在那个路径下,它将无法工作。
  • 这基本上是正确的,但我不确定 Process.Start 是否解析 %userprofile% 。此外,默认情况下,Chrome.exe 未安装在您指定的路径中。
  • @EricJ.我的 Chrome 安装在那里。

标签: c# process environment-variables


【解决方案1】:

我认为Process.Start 不会为您扩展环境变量。试试这个:

var path = Environment.ExpandEnvironmentVariables(@"%userprofile%\AppData\Local\Google\Application\chrome.exe");
Process.Start(path);

【讨论】:

    【解决方案2】:

    试试这个link 来启动外部程序 也试试这个Similar Question on stackoverFlow

    这是一个例子

     string winpath = Environment.GetEnvironmentVariable("windir");
     string path = System.IO.Path.GetDirectoryName(
                  System.Windows.Forms.Application.ExecutablePath);
    
     Process.Start(winpath + @"\Microsoft.NET\Framework\v1.0.3705\Installutil.exe",
     path + "\\MyService.exe");
    

    在你的情况下,在上面列出所有 使用命名空间 的地方写下以下内容

            using System.Diagnostics;
            using System;
    

    那么然后在你的代码中直接写上面的代码...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      相关资源
      最近更新 更多