25

This is the code. I just wanna test the library of System.ServiceProcess library.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hi");
            var srv = new ServiceController("MyService");
            Console.WriteLine("MyService Status {0}", srv.Status);
            if (srv.Status != ServiceControllerStatus.Running)
                srv.Start();
            System.Threading.Thread.Sleep(1000000);
        }
    }
}

However, when I run the C# code, its says:

Error 1 The type or namespace name 'ServiceProcess' does not exist in the namespace 'System' (are you missing an assembly reference?)

What went wrong?

Mickey
  • 943
  • 1
  • 19
  • 41
user1535147
  • 1,325
  • 5
  • 14
  • 21
  • 1
    `Are you missing an assembly reference?` The error is guiding you toward the solution already. Look up [`ServiceController `](http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v=vs.110).aspx) and you'll see this: `Assembly: System.ServiceProcess (in System.ServiceProcess.dll)` – Remus Rusanu Nov 04 '13 at 07:49

4 Answers4

63

The System.ServiceProcess namespace belongs to System.ServiceProcess.dll and it isn't added as a reference by default.

For this, in the solution window, right click on "References" and choose "Add Reference..". Go to the .NET tab, and double click on System.ServiceProcess.dll.

enter image description here

This assembly is probably in the folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.

riQQ
  • 9,878
  • 7
  • 49
  • 66
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
7

Install the NuGet Package System.ServiceProcess.ServiceController

6

You need to add a reference to the corresponding .dll as well.

Right click on the project -> Add Reference -> Assemblies -> Framework -> System.ServiceProcess

walther
  • 13,466
  • 5
  • 41
  • 67
3

you should add this from framework list Right click on the project -> Add Reference -> search under "Assemblies" -> select->OK

enter image description here

reza.cse08
  • 5,938
  • 48
  • 39