简体   繁体   中英

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.

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. The goal is to send a command to my running C# application that will load my script, automating the process even further.

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.

Is it possible to create an external hook in 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.

There are multiple ways of passing information between applications as you can see from the link Ian Kemp posted in the comments. If you're looking for something simple, you could just use the Windows command line to create a text file in a specific folder. Your C# application could then monitor that folder for new files, read the content, delete the file and perform the required actions.

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. 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. You'll need to add using System.IO; for the 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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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