C# 打开文件并传递参数给关联的应用程序
我正在尝试启动为指定附加参数的扩展注册的默认应用程序:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "myargument";
p.FileName = "file.ext";
Process.Start(p);
应用程序开始正确打开指定的文件。 问题是它只获取一个参数(文件名),完全忽略附加的“参数”。
可以做我想做的事吗? 我做错了什么吗?
预先感谢您的帮助,
保罗
I am trying to launch the default application registered for an extension specifying an additional argument:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "myargument";
p.FileName = "file.ext";
Process.Start(p);
The application starts correctly opening the specified file.
The problem is that it is getting just one parameter (the name of the file), totally ignoring the additional "Arguments".
Is it possible to do what I want?
Am I doing something wrong?
Thanks in advance for any help,
Paolo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这是预料之中的。在幕后,Windows 会在注册表中查找默认应用程序并创建一个新进程并将您的文件名传递给它。如果我转到命令提示符并输入“filename.ext argument”,我会得到相同的行为,即我的参数不会传递给应用程序。
您可能需要做的是通过查看注册表自行找到默认应用程序。然后您可以使用参数启动该过程,而不是尝试通过文件类型关联启动。这里有一个关于如何在注册表中查找默认应用程序的答案:
查找在 Windows 上打开特定文件类型的默认应用程序
I believe this is expected. Behind the scenes, Windows is finding the default application in the registry and creating a new process and passing your file name to it. I get the same behavior if I go to a command prompt and type "filename.ext argument", that my arguments are not passed to the application.
What you probably need to do is find the default application yourself by looking in the registry. Then you can start that process with arguments, instead of trying to start by filetype association. There is an answer here on how to find the default application in the registry:
Finding the default application for opening a particular file type on Windows
我认为更简单的方法是使用 cmd 命令
编辑:
我不知道它是否适用于参数,但这正是我正在寻找的启动关联程序的方法...
I think an easier method is using the cmd command
EDIT:
I don't know if it works with arguments, but it is what I was looking for to launch an associated program...
你的“论点”到底是什么,它有空格、反斜杠等吗?
是否有任何原因导致您无法启动应用程序,然后在参数中使用扩展名和参数?
what exactly is your "argument", does it have spaces, backslash, etc?
Is there any reason why you cant start the app, then use the extension and arguments in your arguments?