Jacob Krumholz
Jacob Krumholz

Reputation: 53

Error CS0116: A namespace cannot directly contain members such as fields or methods

Ok so im trying to make a program that checks if a program is currently running. It is giving me a error when ever i declare a void. I am new to C# so im sorry if its a stupid.

using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.VisualBasic.ApplicationServices;

namespace IsProgramRunning
{
    private void IsRunning()
    {
        Process[] pname = Process.GetProcessesByName("VLC Player");
        if (pname.Length > 0)
        {
            MessageBox.Show("Process Running");
        }
        else
        {
            MessageBox.Show("Process Not running");
        }
        System.Threading.Thread.Sleep(5 * 1000);
    }
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
        new Service1()
        };
        ServiceBase.Run(ServicesToRun);


    }
}

If im going about this all wrong and there is a easy way to do it in c++ that would be good to

Upvotes: 2

Views: 26803

Answers (1)

TheGeneral
TheGeneral

Reputation: 81583

To have instance members and methods, you need a class. You have confused a namespace with a class

namespace MyAwesomeNameSpace
{
   public class ProgramRunningHelper
   {
       // put your class code here

   }
}

Compiler Error CS0116

A namespace cannot directly contain members such as fields or methods.

A namespace can contain other namespaces, structs, and classes.

Upvotes: 7

Related Questions