简体   繁体   English

C# 中的 ServiceController 不采用 arguments

[英]ServiceController in C# not taking arguments

Good evening, I am fairly new in C# programming you can not even call me a noob.晚上好,我是 C# 编程的新手,你甚至不能称我为菜鸟。

I try to find my way and sadly I have no way for education other than the internet and stack overflow (thanks for stack overflow.)我试图找到自己的方式,可悲的是,除了互联网和堆栈溢出之外,我没有其他教育途径(感谢堆栈溢出。)

The problem: I want to start and stop a service in C# working with Net 4.0 here.问题:我想在这里使用 Net 4.0 启动和停止 C# 中的服务。

I have added a reference to System.ServiceProcess and I am set the following in the top of the program我添加了对 System.ServiceProcess 的引用,并在程序顶部设置了以下内容

using System;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

Now I have the following function for me:现在我有以下 function 给我:

 // Below is the service start and stop code

        public void ServiceStart(string SVName)
        {
            ServiceController service = new ServiceController(SVName);
            service.Start();
            service.WaitForStatus(ServiceControllerStatus.Running);
        }
        public void ServiceStop(string SVName)
        {
            ServiceController service = new ServiceController(SVName);
            service.Stop();
            service.WaitForStatus(ServiceControllerStatus.Running);
        }

// End of Service Restart Code

The problem I have is that the function gives me 4 errors 2 errors repeat for the "Stop" function.我遇到的问题是 function 给了我 4 个错误 2 个错误重复“停止”function。

'FM_MW.ServiceController' does not contain a definition for 'Start' and no accessible extension method >'Start' accepting a first argument of type 'FM_MW.ServiceController' could be found (are you missing a >using directive or an assembly reference?) 'FM_MW.ServiceController' 不包含'Start' 的定义,并且没有可访问的扩展方法>'Start' 接受'FM_MW.ServiceController' 类型的第一个参数(您是否缺少>using 指令或程序集引用? )

and

FM_MW.ServiceController' does not contain a definition for 'WaitForStatus' and no accessible extension >method 'WaitForStatus' accepting a first argument of type 'FM_MW.ServiceController' could be found (are >you missing a using directive or an assembly reference?) FM_MW.ServiceController' 不包含 'WaitForStatus' 的定义,并且没有可访问的扩展 > 可以找到接受“FM_MW.ServiceController”类型的第一个参数的方法“WaitForStatus”(>您是否缺少 using 指令或程序集引用?)

These 2 errors repeat for the stop function as well.停止 function 也会重复这 2 个错误。 Please help me I really have no idea anymore I am sitting in front of this problem for 4 hours now I know basically nothing and all I do is try to learn through the documentation and stack overflow.请帮助我,我真的不知道我在这个问题面前坐了 4 个小时,现在我基本上什么都不知道,我所做的就是尝试通过文档和堆栈溢出来学习。

Addition: Oh I am sorry I should have explained more about my program补充:哦,对不起,我应该更多地解释我的程序

The program Needs to stop a service then he updates files, then he checks the files updates the database and then he should restart the stopped services.程序需要停止服务然后他更新文件,然后他检查文件更新数据库,然后他应该重新启动停止的服务。

I need these functions to stop a service so that I can update files.我需要这些功能来停止服务,以便我可以更新文件。

I found the Problem myself after long long search.经过长时间的搜索,我自己发现了问题。

It seems I created a function somewhere in the code with the auto fix.看来我在代码中的某处使用自动修复创建了 function。

The serviceController tried to access that code. serviceController 试图访问该代码。 and not the system.而不是系统。

Means I had意味着我有

Form_ServiceController [as my own function] Form_ServiceController [作为我自己的功能]

and not并不是

System.ServiceProcess.ServiceController System.ServiceProcess.ServiceController

That was the problem after I deleted my rogue code snippet everything worked as intended.这就是我删除我的流氓代码片段后的问题,一切都按预期工作。

!! !!

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

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