使用命令行参数启动文件而不知道exe的位置?

时间:2021-10-22 23:17:05

Here's the situation: I am trying to launch an application, but the location of the .exe isn't known to me. Now, if the file extension is registered (in Windows), I can do something like:

现在的情况是:我正在尝试启动一个应用程序,但是.exe的位置我不知道。现在,如果文件扩展名已注册(在Windows中),我可以做以下事情:

Process.Start("Sample.xls");

However, I need to pass some command line arguments as well. I couldn't get this to work

但是,我还需要传递一些命令行参数。我不能让这个工作。

   Process p = new Process();
   p.StartInfo.FileName = "Sample.xls";
   p.StartInfo.Arguments = "/r";  // open in read-only mode
   p.Start();

Any suggestions on a mechanism to solve this?

对于解决这个问题的机制有什么建议吗?

Edit @ aku

编辑@鲔

My * search skills are weak; I did not find that post. Though I generally dislike peering into the registry, that's a great solution. Thanks!

我的*搜索技能很弱;我没有找到那篇文章。虽然我通常不喜欢查看注册表,但这是一个很好的解决方案。谢谢!

2 个解决方案

#1


4  

Using my code from this answer you can get command associated with xls extension. Then you can pass this command to Process.Start method.

使用这个答案中的代码,您可以获得与xls扩展相关联的命令。然后您可以将该命令传递给进程。Start方法。

#2


2  

If you query the registry, you can retrieve the data about the registered file type and then call the app directly passing the command line arguments. See Programmatically Checking and Setting File Types for an example of retrieving shell information for a file type.

如果查询注册表,可以检索有关已注册文件类型的数据,然后调用该应用程序直接传递命令行参数。查看以编程方式检查和设置文件类型,以获取文件类型的shell信息。

#1


4  

Using my code from this answer you can get command associated with xls extension. Then you can pass this command to Process.Start method.

使用这个答案中的代码,您可以获得与xls扩展相关联的命令。然后您可以将该命令传递给进程。Start方法。

#2


2  

If you query the registry, you can retrieve the data about the registered file type and then call the app directly passing the command line arguments. See Programmatically Checking and Setting File Types for an example of retrieving shell information for a file type.

如果查询注册表,可以检索有关已注册文件类型的数据,然后调用该应用程序直接传递命令行参数。查看以编程方式检查和设置文件类型,以获取文件类型的shell信息。