【问题标题】:Error CS0116: A namespace cannot directly contain members such as fields or methods错误 CS0116:命名空间不能直接包含字段或方法等成员
【发布时间】:2020-02-15 01:39:29
【问题描述】:

好的,我正在尝试制作一个程序来检查程序当前是否正在运行。每当我宣布无效时,它都会给我一个错误。我是 C# 新手,如果它很愚蠢,我很抱歉。

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);


    }
}

如果我把这一切都错了,并且有一个简单的方法可以在 c++ 中做到这一点,这将是一件好事

【问题讨论】:

  • 你不能像这样直接在命名空间中创建函数,函数必须包含在一个类中

标签: c#


【解决方案1】:

要拥有实例成员和方法,您需要class。您将 namespaceclass 混淆了

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

   }
}

Compiler Error CS0116

命名空间不能直接包含字段或方法等成员。

命名空间可以包含其他命名空间、结构和类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多