让上下文菜单条目在运行时将参数传递给 Windows 服务

发布于 2024-10-16 05:30:36 字数 1899 浏览 3 评论 0原文

我有一个简单的 Windows 服务,名为“MyService”,使用 WCF 构建,在启动时将注册表项添加到“HKEY_CLASSES_ROOT\Folder\Shell\{ContextMenuEntryName}”,从而允许在 Windows 中的任何位置右键单击菜单有一个名为:{ContextMenuEntryName}。单击此条目时执行的目标是另一个程序,例如“{Path}\serviceclient.exe %1”。 “%1”为我提供了调用该上下文菜单的 Windows 文件夹的路径。然后,该程序获取该值并通过创建服务代理并调用其方法将其传递给我的服务。

其 OnStart 方法的 WindowsService 代码如下:

    protected override void OnStart(string[] args)
    {            
            if (_serviceHost != null)
            {
                _serviceHost.Close();
            }

            // Add registry entry for context menu option                             System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            string contextMenuCommandPath =
                Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("MyService")) +
                "serviceclient\\bin\\Debug\\serviceclient.exe %1";
            _contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "Folder");
            _contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "*");

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            _serviceHost = new ServiceHost(typeof(MyService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            _serviceHost.Open();           
    }

在向其添加名为“MyService”的服务引用后,serviceclient.exe 具有以下代码:

        // Instantiate service proxy
        var myServiceClient = new MyService.MyServiceClient();
        // Execute monitor target based on path specified (or default).
        myServiceClient .Monitor(args.Length > 0 ? args[0] : string.Empty);

当我单击上下文菜单时,我想直接将参数传递给 Windows 服务本身输入而不是调用另一个执行相同操作的单独程序。

是否可以让上下文菜单条目在 WCF 运行时直接将信息传递给 WCF 中的 Windows 服务?

I have a simple windows service called lets say "MyService", built using WCF that on start adds a registry entry to "HKEY_CLASSES_ROOT\Folder\Shell\{ContextMenuEntryName}" thus allowing for the right click menu anywhere in windows to have an entry named : {ContextMenuEntryName}. The target executed on clicking this entry is another program e.g. "{Path}\serviceclient.exe %1". The "%1" gives me the path of the windows folder that that context menu has been invoked on. This program then takes that value and passes it to my service by creating a proxy of the service and invoking its method.

The WindowsService code for its OnStart method is as follows:

    protected override void OnStart(string[] args)
    {            
            if (_serviceHost != null)
            {
                _serviceHost.Close();
            }

            // Add registry entry for context menu option                             System.IO.Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
            string contextMenuCommandPath =
                Environment.CurrentDirectory.Substring(0, Environment.CurrentDirectory.IndexOf("MyService")) +
                "serviceclient\\bin\\Debug\\serviceclient.exe %1";
            _contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "Folder");
            _contextMenu.AddContextMenu(ContextMenuName, contextMenuCommandPath, "*");

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            _serviceHost = new ServiceHost(typeof(MyService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            _serviceHost.Open();           
    }

The serviceclient.exe has the following code after adding a service reference named "MyService" to it:

        // Instantiate service proxy
        var myServiceClient = new MyService.MyServiceClient();
        // Execute monitor target based on path specified (or default).
        myServiceClient .Monitor(args.Length > 0 ? args[0] : string.Empty);

I would like to directly pass arguments to the Windows service itself when I click on the context menu entry instead of invoking another separate program that does the same thing.

Would it be possible to have the context menu entry directly pass information to a Windows Service in WCF while it is running?

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

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

发布评论

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

评论(2

椵侞 2024-10-23 05:30:36

乍一看不像,但这是一个复杂的主题。请参阅创建快捷菜单处理程序

It doesn't look like it on first glance, but it's a complicated subject. See Creating Shortcut Menu Handlers.

懒的傷心 2024-10-23 05:30:36

如果您在 Windows 服务中实现了命名管道服务器,也许您可​​以直接从上下文菜单命令发送文件路径数据,如下所示:

echo %1 > \\.\pipe\{pipe-name}

但是,您不能使用标准 WCF NetNamedPipeBinding 来实现管道服务器,因为 所有 .NET 特定协议要求都融入到该绑定中,因此您需要使用 System.IO.Pipes 类或编写自定义 WCF 传输来完成此操作它只从管道接收原始消息。

If you implemented a named pipe server inside your Windows service maybe you could send the file path data directly from the context menu command with something like:

echo %1 > \\.\pipe\{pipe-name}

However, you can't use the standard WCF NetNamedPipeBinding to implement the pipe server, because of all the .NET-specific protocol requirements baked into that binding, so you would need to do it either using the System.IO.Pipes classes, or write a custom WCF transport which just receives raw messages from the pipe.

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