简体   繁体   English

如何与新进程共享信息(除了命令行参数)?

[英]How to share information with a new process (besides the command-line arguments)?

I'm starting a new process with: 我正在开始一个新的过程:

process.Start();

I know I can pass strings to it using: 我知道我可以使用以下方式将字符串传递给它:

process.StartInfo.Arguments = ...

But I want to share more than just a couple of simple strings – a byte array etc. How do I do that? 但是我不仅要共享几个简单的字符串(字节数组等),还应该共享。我该怎么做?

EDIT: This is going to be installed as a ClickOnce application, so I don't really know where it will be installed in the file system, and I want to keep it as simple as possible so no files will remain after an uninstall. 编辑:这将作为ClickOnce应用程序安装,所以我真的不知道它将在文件系统中安装的位置,并且我想使它尽可能简单,以便在卸载后不会保留任何文件。 So where do I put this data? 那我该把数据放在哪里呢?

Take a look at memory mapped files - they allow you to share data between processes. 看一下内存映射文件 -它们使您可以在进程之间共享数据。

Alternatives are regular files, the registry, communicating over sockets and more. 替代方法是常规文件,注册表,通过套接字进行通信等。

If the array is small you can just base64 encode it. 如果数组很小,则可以对它进行base64编码。 Other options consist of saving data to the file or using named pipes 其他选项包括将数据保存到文件或使用命名管道

You may want to use Anonymous Pipes . 您可能要使用匿名管道

Anonymous pipes offer less functionality than named pipes, but also require less overhead. 匿名管道提供的功能少于命名管道,但也需要较少的开销。 You can use anonymous pipes to make interprocess communication on a local computer easier. 您可以使用匿名管道来简化本地计算机上的进程间通信。 You cannot use anonymous pipes for communication over a network. 您不能使用匿名管道通过网络进行通信。

You can create a temp file, write all required data into it and send result file name as a command line argument. 您可以创建一个临时文件,将所有必需的数据写入其中,然后将结果文件名作为命令行参数发送。 When the second application run it will read the content of file and delete it. 当第二个应用程序运行时,它将读取文件的内容并将其删除。

if you can encode the byte array properly (eg base64), you can redirect stdin of the target process and stream it through that: 如果您可以正确编码字节数组(例如base64),则可以重定向目标进程的stdin并通过它进行流传输:

http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM