从不同PC的C:\ Users \ user \ AppData \ Roaming \ ect运行.exe文件

时间:2017-05-18 07:02:45

标签: c# winforms

我有一个Windows From应用程序,可以在语音命令上运行多项操作。

在某个命令上我想启动Spotify,因为您可能知道Spotify已安装在C:\\Users\\Danny\\AppData\\Roaming\\Spotify\\Spotify.exe中。

只要我的应用程序安装在自己的PC上,这不是问题。但是,当我想在不同的PC上运行我的应用程序时,这当然不会起作用。

有没有办法可以使用通用路径来运行此应用程序?

1 个答案:

答案 0 :(得分:2)

使用Envoirment.SpecialFolder

如果您使用Environment.SpecialFolder而不是为当前用户获取路径,那么您不必手动输入路径:

此示例将获取appdata文件夹的路径。从那里你可以进一步进入文件结构,就像我使用Path.Comine()一样,以防止任何路径组合错误。

  // Get appdata folder path for every user
  string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  // Your extension to Spotify.exe
  string extentionToPath = "Spotify\\Spotify.exe";
  // Finalpath, combine the appdata with your own extention.
  string finalPath = Path.Combine(appDataPath, extentionToPath);

  // one line to get the path
 string finalPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\\Roaming\\Spotify\\Spotify.exe");

现在,您可以通过以下方式开始此过程:

Process.Start(finalPath);