gpt4 book ai didi

c# - 错误 CS0116 : A namespace cannot directly contain members such as fields or methods

转载 作者:行者123 更新时间:2023-12-02 19:40:38 29 4
gpt4 key购买 nike

好的,所以我正在尝试制作一个程序来检查程序当前是否正在运行。每当我宣布无效时,它都会给我一个错误。我是 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++ 中做到这一点,这对

最佳答案

要拥有实例成员和方法,您需要一个 class .你混淆了 namespaceclass

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.

关于c# - 错误 CS0116 : A namespace cannot directly contain members such as fields or methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60235340/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com