简体   繁体   English

从命令行触发 C# windows 表单中的操作?

[英]Trigger an action in C# windows form from command line?

I've done some searching but conflicting terms for my question makes it hard to find what I'm looking for, if it's even possible.我已经为我的问题进行了一些搜索,但相互矛盾的术语使我很难找到我正在寻找的东西,如果它甚至可能的话。

What I would like to do is to run a windows form C# program and have it "listen" for external commands, ideally sent somehow through a windows command line.我想做的是从 C# 程序运行 windows 程序并让它“监听”外部命令,理想情况下通过 windows 命令行以某种方式发送。

The use case is that I have some automation software that I have to run with some test equipment software.用例是我有一些自动化软件,我必须与一些测试设备软件一起运行。 During different points of my test, I need to run a script in my automation software (manually selecting the script and pressing a button).在我测试的不同阶段,我需要在我的自动化软件中运行一个脚本(手动选择脚本并按下一个按钮)。 However, I can include steps in my sequence in the test software that fires windows command line commands.但是,我可以在触发 windows 命令行命令的测试软件中包含我的序列中的步骤。 The goal is to send a command to my running C# application that will load my script, automating the process even further.目标是向我正在运行的 C# 应用程序发送一个命令,该应用程序将加载我的脚本,进一步自动化该过程。

While I could simply re-write a smaller version of my automation software that is single-run use and uses command line arguments, there is a bunch of pre-amble that the automation software that connects to some hardware that would make the process take longer than working in the automation software manually.虽然我可以简单地重写我的自动化软件的较小版本,它是单次运行使用并使用命令行 arguments,但有一堆前导码表明,连接到某些硬件的自动化软件会使该过程花费更长的时间而不是手动在自动化软件中工作。

Is it possible to create an external hook in C#?是否可以在 C# 中创建外部挂钩? I'm not looking for a full code answer.我不是在寻找完整的代码答案。 I mostly just need the name of this feature so that I can look it up and learn it properly.我主要只需要此功能的名称,以便我可以查找并正确学习它。

Hopefully I've made this clear enough.希望我已经说得够清楚了。

Edit: As an extra bit of information, I only have access to the source of my automation software, not the testing software.编辑:作为额外的信息,我只能访问我的自动化软件的源代码,而不是测试软件。 The only options I have for the testing software is to export a file (which I may end up having to poll a file for changes in the automation software), or running a windows CMD command.我对测试软件的唯一选择是导出文件(我可能最终不得不轮询文件以了解自动化软件中的更改),或者运行 windows CMD 命令。

There are multiple ways of passing information between applications as you can see from the link Ian Kemp posted in the comments.从 Ian Kemp 在评论中发布的链接中可以看出,应用程序之间传递信息的方式有多种。 If you're looking for something simple, you could just use the Windows command line to create a text file in a specific folder.如果您正在寻找简单的东西,您可以使用 Windows 命令行在特定文件夹中创建文本文件。 Your C# application could then monitor that folder for new files, read the content, delete the file and perform the required actions.然后,您的 C# 应用程序可以监视该文件夹中的新文件、读取内容、删除文件并执行所需的操作。

Here's an example command for creating the file from your script.这是从脚本创建文件的示例命令。 I've used the date and time for the file name to try to make it unique.我已使用文件名的日期和时间来尝试使其唯一。 As long as there's a short delay between files being created this should be fine.只要创建文件之间有短暂的延迟,这应该没问题。 Otherwise you may want to name the files another way, maybe incremental numbers?否则,您可能想以另一种方式命名文件,也许是递增数字? On my system the date and time are formatted as dd/MM/yyyy HH:mm:ss.在我的系统上,日期和时间的格式为 dd/MM/yyyy HH:mm:ss。 This example replaces the forward slashes and colons with dashes to make a valid file name.此示例将正斜杠和冒号替换为破折号以生成有效的文件名。 You may need to adjust this dependent on the date and time format on your system.您可能需要根据系统上的日期和时间格式进行调整。

echo some command > C:\SomeFolder\%date:/=-%-%time::=-%.txt

Then in your Win Forms C# app you could do something like this.然后在你的 Win Forms C# 应用程序中你可以做这样的事情。 You'll need to add using System.IO;您需要using System.IO; for the FileSystemWatcher class.对于FileSystemWatcher class。

private void Form1_Load(object sender, EventArgs e)
{
    FileSystemWatcher fileWatcher = new FileSystemWatcher()
    {
        Path = @".\", // Path of the directory you want to monitor
        Filter = "*.txt", // Filter for the file names
        NotifyFilter = NotifyFilters.LastWrite, // Raise an event when the write time of a file changes
        EnableRaisingEvents = true
    };
    fileWatcher.Changed += FileWatcher_Changed; // Subscribe to the file changed event
}

private void FileWatcher_Changed(object sender, FileSystemEventArgs e)
{
    // Read the contents of the file and delete it
    string command = File.ReadAllText(e.FullPath).Trim(); // ReadAllText() may not be the best option, but it's just an example
    File.Delete(e.FullPath);

    // Check the contents of the file and decide what to do
    if (command == "some command")
    {
        DoSomething();
    } 
    else if (command == "something else")
    }
        DoSomethingElse();
    }
}

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

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