简体   繁体   English

运行时如何将命令行参数传递给C#Windows Forms应用程序

[英]How to pass a command line argument to a C# windows forms application while it is running

I have ac# windows forms application framework 2.0 我有AC#Windows Forms应用程序框架2.0

I want to be able to pass a command from another process or from command line to my windows forms application while my application is running. 我希望能够在应用程序运行时将命令从另一个进程或命令行传递到Windows窗体应用程序。 How can I send and how can I receive? 我如何发送和如何接收?

I know it can be done thought IPC but I dont want to go there just yet as this passing the argument feature will be rarely used and I am trying make minimal changes in the windows forms application as possible. 我知道可以通过IPC来完成,但是我现在还不想去那儿,因为这种传递参数的功能将很少使用,我正在尝试在Windows窗体应用程序中进行最小的更改。

edit: Right now the I run windows forms application by double-clicking icon..Can I make it run from command line as a process (console application???) and send commands from command-line? 编辑:现在,我通过双击图标来运行Windows窗体应用程序。我可以使其从命令行作为进程运行(控制台应用程序???),并从命令行发送命令吗?

Edit: Further clarification: The command is entered by user. 编辑:进一步说明:该命令由用户输入。

You can't pass arguments to an already-running program. 您不能将参数传递给已经运行的程序。

The standard method for doing this, as you mentioned, is starting a new instance to receive the arguments, detecting the already-running instance, and sending it commands via IPC. 正如您提到的,执行此操作的标准方法是启动一个新实例以接收参数,检测已在运行的实例,然后通过IPC发送命令。

As mentioned in the comments for some of the other answers, it is possible to have an application continually reading data from the command line. 如其他一些答案的注释中所述,有可能使应用程序从命令行连续读取数据。 This is called the Standard Input stream, and is available in .NET via the Process.StandardInput class, documented here . 这称为“ 标准输入”流,可通过.NETDocument中的Process.StandardInput类在.NET中使用,该文档在此处记录 Likewise, applications also have standard output and standard error streams. 同样,应用程序也具有标准输出和标准错误流。

Standard input can be leveraged from other applications, but ultimately that comes down to implementing a form of IPC. 可以从其他应用程序中利用标准输入,但是最终归结为实施一种IPC形式。

You can use the processes standard input stream as an alternative, or a named pipe, memory mapped file, etc. 您可以使用流程标准输入流作为替代,也可以使用命名管道,内存映射文件等。

There are lots of ways for two programs to share data or talk to each other. 两个程序有很多共享数据或互相交谈的方法。

You cannot specify/change command line parameters after the program has already started. 程序启动后,您将无法指定/更改命令行参数。

Update: 更新:

so I see many C++ application running from command-line and user keeps passing commands from there. 因此,我看到许多C ++应用程序从命令行运行,并且用户不断从那里传递命令。

They are doing one of the options that I've listed here (or something similar that I haven't listed; there are a LOT of ways for different programs to talk to each other). 他们正在执行我在此处列出的选项之一(或我未列出的类似选项;不同程序之间有很多相互交谈的方式)。

Is there a way to do the same with ac# windows forms application? 是否可以使用ac#Windows Forms应用程序执行相同操作?

All of the above should work just in a windows forms application. 以上所有内容仅应在Windows窗体应用程序中工作。 Some may be easier than others, but they're all possible. 有些可能比其他的要容易,但它们都是可能的。

or I need to make it a console application and then somehow launch the UI part of it? 还是我需要使其成为一个控制台应用程序,然后以某种方式启动它的UI部分?

well, that's actually what's already happening; 好吧,这实际上已经在发生; it's just all behind the scenes. 一切都在幕后。 Your forms app will still have a standard input; 您的表格应用仍将具有标准输入; it can be launched from a shell; 它可以从外壳启动; it can be given command line arguments; 可以给它命令行参数; it can access pipes, files, sockets, etc. 它可以访问管道,文件,套接字等。

Uhm, when your application starts up, let it see if this application is already running, if it is, then just take arguments and store that information into cache folder and exit second instance of your application. 嗯,当您的应用程序启动时,让它查看该应用程序是否已经在运行,如果正在运行,则只需接受参数并将该信息存储到缓存文件夹中,然后退出应用程序的第二个实例。

First instance of your application should be monitoring changes in that folder and if anything changes, fetch that data and use it. 应用程序的第一个实例应该是监视该文件夹中的更改,如果有任何更改,请获取该数据并使用它。

And please don't tell me it's too hacky or "unefficent" because this simply gets things done...no need to do some overly-complicated stuff. 而且,请不要告诉我它太过hacky或“效率低下”,因为这只是使事情完成了……不需要做一些过于复杂的事情。

I basically started a new TCP link between my windows forms application and client application. 我基本上在Windows窗体应用程序和客户端应用程序之间开始了新的TCP链接。

My windows application becomes a socket server and client as client, so whenever user wants to send in a command he can establish the connection and send it in. 我的Windows应用程序成为套接字服务器,并作为客户端成为客户端,因此,每当用户要发送命令时,他都可以建立连接并将其发送。

Thanks for all the valuable input...I did learn a few things. 感谢您提供的所有宝贵信息...我确实学到了一些东西。

No matter what you do, you're going to have to make some changes in your Windows Forms app, I think, though you might be able to do some magic by sending win32 messages to it via the win32 SendMessage() / PostMessage() functions. 我认为,无论您做什么,都必须在Windows Forms应用程序中进行一些更改,尽管您可以通过通过win32 SendMessage() / PostMessage()向其发送win32消息来做一些魔术。职能。 That would work as long as its a message your winforms app is expecting. 只要您的winforms应用程序期望它发出一条消息,它就可以工作。

You'll be making friends with the interop stuff in the process. 在此过程中,您将与互操作性材料成为朋友。 Some links for using win32 messages as an IPC mechanism: 一些将win32消息用作IPC机制的链接:

Alternatively, use System.IO.Pipes . 或者,使用System.IO.Pipes

The first instance that starts is the "server". 启动的第一个实例是“服务器”。 It should create a named pipe by creating an instance of NamedPipeServerStream and listen on it. 它应该通过创建NamedPipeServerStream的实例来创建命名管道并对其进行侦听。

Anybody that wants to talk to the server -- whether its other instances of your app or a command line app, etc., needs to create an instance of NamedPipeClientStream and connect to it. 任何想与服务器对话的人-无论是您的应用程序的其他实例还是命令行应用程序等,都需要创建NamedPipeClientStream的实例并连接到它。

Here's an example: http://www.switchonthecode.com/tutorials/dotnet-35-adds-named-pipes-support 这是一个示例: http : //www.switchonthecode.com/tutorials/dotnet-35-adds-named-pipes-support

Your win32 app will need to monitor the server pipe to handle inbound message on the pipe. 您的win32应用将需要监视服务器管道以处理管道上的入站消息。

Any application have command line parameter - so if you just want to pass them once at startup - just do so. 任何应用程序都有命令行参数-因此,如果您只想在启动时传递一次-只需这样做。

Otherwise you want console application that shows forms - ie see how to run a winform from console application? 否则,您需要显示表单的控制台应用程序-即查看如何从控制台应用程序运行winform? .

Note: While having console and window at the same time is possible, user experience could be confising. 注意:虽然可以同时拥有控制台和窗口,但用户体验可能会混乱。 Consider adding simple textbox "command" to your form to input commands. 考虑将简单的文本框“命令”添加到表单中以输入命令。 Ie VisualStudio have several "command" windows as an example - "Immediate" where you can execute code agains running program, "searc"/"command" - go to search text box in toolbar (or Ctrl+/) and type >open . 例如,VisualStudio有几个“命令”窗口作为示例-“立即”,您可以在其中再次运行运行程序的代码,“搜索” /“命令”-转到工具栏(或Ctrl + /)中的搜索文本框,然后键入> open。

暂无
暂无

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

相关问题 使用命令行参数从批处理文件运行Windows Forms Application(C#.NET 4.0) - Running Windows Forms Application (C# .NET 4.0) from batch file with Command Line Arguments 如何在.net Windows应用程序中传递命令行参数 - How to pass command line argument in .net windows application (如何)你能否在C#中将空格作为命令行参数传递? - (How) can you pass whitespace as a command line argument in C#? 如何使用C#在Python中将字符串作为命令行参数传递 - How to pass string as command line argument in python using C# 如何在c#windows应用程序中的表单之间传递值? - How to pass values between forms in c# windows application? 从命令行运行7zip的C#应用​​程序-如何从打开命令行窗口停止7zip? - C# application running 7zip from command line - How to stop 7zip from opening command line windows? C# - 是否可以创建一个可以从带命令行的命令行运行的Windows窗体应用程序? - C# - Is it possible to create a Windows Forms application that can run from the command line with parameters? 在运行命令提示符.exe应用程序时,将C#WPF应用程序中的特定行定义为超时 - Define timeout to specific line in C# WPF application while running command prompt .exe application 在Windows 7上以Parallels 8错误运行C#Windows窗体应用程序 - Running a C# Windows Forms application on Windows 7 in Parallels 8 error C#Windows窗体应用程序逐行读取文本框 - C# Windows Forms Application Read Textbox Line by Line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM