是否可以通过关联文件将命令行参数传递给可执行文件?

时间:2022-12-10 23:15:21

I have a program that passes command-line arguments to an associated file (i.e. associated file extension) of an executable. The executable never receives the arguments. However, if I start the executable directly and pass it both the path to the associated file and the arguments, then it receives both the file path and the arguments.

我有一个程序将命令行参数传递给可执行文件的关联文件(即关联文件扩展名)。可执行文件永远不会收到参数。但是,如果我直接启动可执行文件并将其传递给关联文件和参数的路径,那么它将接收文件路径和参数。

  • Operating System: Windows XP
  • 操作系统:Windows XP

  • Programming Language: C#
  • 编程语言:C#

I am starting the associated file using:

我使用以下命令启动相关文件:

System.Diagnostics.Process.Start(filepath, arguments)

Thanks in advance for all assistance.

在此先感谢您的所有帮助。

-EDIT

Basically, I have a programming language interpreter that needs to receive command-line arguments passed to it by a C# program.

基本上,我有一个编程语言解释器,需要接收由C#程序传递给它的命令行参数。

If I start a code file using the C# program, the interpreter will start, but not receive the command-line arguments that were passed to the code file by the C# program.

如果我使用C#程序启动代码文件,解释器将启动,但不会接收由C#程序传递给代码文件的命令行参数。

So there are a total of three files:

所以总共有三个文件:

  1. the interpreter
  2. the code file
  3. 代码文件

  4. the program trying to start the code file as though it were a program
  5. 该程序试图启动代码文件,就好像它是一个程序

Also, starting the interpreter directly is not an option, because it is not located at the same file path on every computer.

此外,直接启动解释器不是一个选项,因为它不位于每台计算机上的同一文件路径上。

I hope this is clearer, but I cannot post the source code do to legal restrictions.

我希望这更清楚,但我不能将源代码发布到法律限制。

1 个解决方案

#1


You could try (untested) changing the file association (on the advanced pane) to include %2 %3 etc in the arguments (normally it just includes %1) - however, this involves changes at the client, and (more importantly) the entire idea of passing arguments to a document presumes that you have the same viewer (i.e. that the arguments are sensible).

您可以尝试(未经测试)更改文件关联(在高级窗格上)以在参数中包含%2%3等(通常它只包括%1) - 但是,这涉及客户端的更改,并且(更重要的是)将参数传递给文档的整个想法假定您拥有相同的查看器(即参数是合理的)。

IMO, a better option is to explicitly launch the exe, passing the doc (and the other others) as arguments.

IMO,更好的选择是显式启动exe,将doc(和其他其他)作为参数传递。

Example:

receiver exe (just shows the command arguments received):

receiver exe(只显示收到的命令参数):

static class Program {
    [System.STAThread]
    static void Main(string[] args) {
        System.Windows.Forms.MessageBox.Show(string.Join("|", args));
    }
}

Then: created a "foo.flibble" file, right-click/open and associate with my receiver; went into file associations, "flibble", advanced, "open", edit, and added %2 %3 %4

然后:创建一个“foo.flibble”文件,右键单击/打开并与我的接收器关联;进入文件关联,“轻松”,高级,“开放”,编辑,并添加%2%3%4

Then in a separate exe:

然后在一个单独的exe:

Process.Start(@"c:\foo.flibble", "test of args");

Which shows:

c:\foo.flibble|test|of|args

So this has now passed the extra arguments to the exe via the document. But a lot of client configuration!

所以这已经通过文档传递了额外的参数到exe。但是很多客户配置!

#1


You could try (untested) changing the file association (on the advanced pane) to include %2 %3 etc in the arguments (normally it just includes %1) - however, this involves changes at the client, and (more importantly) the entire idea of passing arguments to a document presumes that you have the same viewer (i.e. that the arguments are sensible).

您可以尝试(未经测试)更改文件关联(在高级窗格上)以在参数中包含%2%3等(通常它只包括%1) - 但是,这涉及客户端的更改,并且(更重要的是)将参数传递给文档的整个想法假定您拥有相同的查看器(即参数是合理的)。

IMO, a better option is to explicitly launch the exe, passing the doc (and the other others) as arguments.

IMO,更好的选择是显式启动exe,将doc(和其他其他)作为参数传递。

Example:

receiver exe (just shows the command arguments received):

receiver exe(只显示收到的命令参数):

static class Program {
    [System.STAThread]
    static void Main(string[] args) {
        System.Windows.Forms.MessageBox.Show(string.Join("|", args));
    }
}

Then: created a "foo.flibble" file, right-click/open and associate with my receiver; went into file associations, "flibble", advanced, "open", edit, and added %2 %3 %4

然后:创建一个“foo.flibble”文件,右键单击/打开并与我的接收器关联;进入文件关联,“轻松”,高级,“开放”,编辑,并添加%2%3%4

Then in a separate exe:

然后在一个单独的exe:

Process.Start(@"c:\foo.flibble", "test of args");

Which shows:

c:\foo.flibble|test|of|args

So this has now passed the extra arguments to the exe via the document. But a lot of client configuration!

所以这已经通过文档传递了额外的参数到exe。但是很多客户配置!