C# ProcessStartInfo:第二个进程不接收任何参数

发布于 2025-01-14 04:09:36 字数 1126 浏览 6 评论 0原文

我试图从一个进程调用来启动另一个进程,并在 ProcessStartInfo 中将启动参数作为单独的参数提供。起始调用使用在 Windows 中注册的 URL 来查找输入到 FileName 中的第二个程序。然后,我向 Arguments 添加一个包含 4 个参数的字符串。据我了解,字符串中的空格表示参数的分隔。

程序 1

//Create starting information
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo() {
    FileName = "urlHandle:",
    Arguments = "/argA valueA /argB valueB"
};

//Start second program
System.Diagnostics.Process.Start(startInfo);

程序 2

//Print received arguments to a file
Environment.GetCommandLineArgs().ToList().ForEach(a => writer.WriteLine(a + ",\t"));

第二个程序按预期启动(意味着 URL 正常工作),但输出不完整。

[第二个程序的.exe路径],

url句柄:,

它包含程序的路径,设置为 FileName 的字符串,但放入 Arguments 中的所有内容都丢失了。

有人知道为什么争论会消失吗?

注 1: 如果我将参数添加到 FileName 中,我会将它们作为一个字符串接收。为了在第二个程序中触发我想要的行为,我必须为其提供多个参数而不是一个。我知道这可以通过从终端手动测试来实现。

注 2: 我正在使用 .Net Framework,因此尝试 .Net Core 中的 ArgumentList 不是一个选项。

I'm trying to make a call from one process to start another, supplying starting arguments as separate parameters in a ProcessStartInfo. The starting call uses a URL registered in Windows to find the second program, entered into FileName. I then add a string containing 4 parameters to Arguments. As I have understood it, the spaces in the string indicate the separation of the arguments.

Program 1

//Create starting information
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo() {
    FileName = "urlHandle:",
    Arguments = "/argA valueA /argB valueB"
};

//Start second program
System.Diagnostics.Process.Start(startInfo);

Program 2

//Print received arguments to a file
Environment.GetCommandLineArgs().ToList().ForEach(a => writer.WriteLine(a + ",\t"));

The second program starts as intended (meaning the URL is working), but the output is incomplete.

[path to .exe of second program],

urlHandle:,

It contains the path to the program, the string set as FileName , but everything put into Arguments is missing.

Does anybody have any ideas why the arguments disappear?

Note 1: If I would add the arguments into the FileName, I would receive them as one string. In order to trigger the behaviour I want in the second program, I must supply it with several parameters instead of one. I know this is possible from testing it manually from the terminal.

Note 2: I'm using .Net Framework, so trying ArgumentList from .Net Core is not an option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-21 04:09:36

经过更多测试后我发现了这个问题。该错误不在于我如何设置和使用 ProcessStartInfo,而在于我使用自定义 URL 协议。

在 Windows 注册表中,定义了可以通过 URL 调用发送的参数数量 ("C:\[我的 .exe 的路径]" "%1")。第一个参数是文件名,因此是通过协议发送的唯一内容。为了发送更多,需要添加“%2”“%3”等。

注意:路径本身变成接收程序中的参数 0,而实际发送的参数从参数 1 开始及以后。

After some more tests I have found the issue. The error does not lie in how I set up and use ProcessStartInfo, but rather in that I am using a custom URL protocol.

In the Windows registry one defines the number of parameters that can be sent via the URL call ("C:\[path to my .exe]" "%1"). The first argument is the Filename, and is as such the only thing sent via the protocol. In order to send more, one is required to add "%2", "%3", etc.

Note: The path itself becomes argument 0 in the receiving program, while the actual sent parameters start at argument 1 and beyond.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文