C# Open File With Associated Application passing arguments

六月ゝ 毕业季﹏ 提交于 2019-12-02 04:56:05
irritate

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

what exactly is your "argument", does it have spaces, backslash, etc?

    Process process = new Process();
    process.StartInfo.FileName = @"C:\process.exe";
    process.StartInfo.Arguments = @"-r -d something else";
    process.StartInfo.CreateNoWindow = false;
    process.StartInfo.UseShellExecute = false;
    process.Start();

Is there any reason why you cant start the app, then use the extension and arguments in your arguments?

I think an easier method is using the cmd command

 void LaunchAssociatedProgram(string filename) {
     Process.Start( @"cmd.exe", "/C start "+ filename );
 }

EDIT:

I don't know if it works with arguments, but it is what I was looking for to launch an associated program...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!