C# 错误:命名空间不能直接包含成员,例如字段或方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13376566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: A namespace cannot directly contain members such as fields or methods
提问by user1823287
I'm new to C# and I'm having trouble solving this error can anyone help me please? This script is to delete an un-needed shortcut then install a new program if it hasn't been installed already.
我是 C# 的新手,我在解决这个错误时遇到了麻烦,有人可以帮我吗?此脚本用于删除不需要的快捷方式,然后安装尚未安装的新程序。
using System;
using WindowsInstaller;
string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcutold = Path.Combine(startMenuDir, @"Ohal\TV AMP (Windows XP Mode).lnk");
if (File.Exists(shortcutold))
File.Delete(shortcutold);
string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcut = Path.Combine(startMenuDir, @"Ohal\TV AMP.lnk");
if (File.Exists(shortcut))
{
Console.WriteLine("Already installed...");
}
else
{
Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
Installer installer = (Installer)Activator.CreateInstance(type);
installer.InstallProduct(@"Y:\LibSetup\TVAMP313\TVAmp v3.13.msi");
}
回答by Habib
Your code should be in a class and then a method. You can't have code under namespace. Something like following.
您的代码应该在一个类中,然后是一个方法。命名空间下不能有代码。像下面这样的东西。
using System;
using WindowsInstaller;
class MyClass //Notice the class
{
//You can have fields and properties here
public void MyMethod() // then the code in a method
{
string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcutold = Path.Combine(startMenuDir, @"Ohal\TV AMP (Windows XP Mode).lnk");
if (File.Exists(shortcutold))
File.Delete(shortcutold);
// your remaining code .........
}
}
回答by Jon Skeet
As Habib says, you need to put code in methods, constructors etc. In this case if the code you want is just what you want as the entry point, you just need:
正如 Habib 所说,您需要将代码放入方法、构造函数等中。在这种情况下,如果您想要的代码正是您想要的入口点,您只需要:
using System;
using WindowsInstaller;
class Program
{
// Or static void Main(string[] args) to use command line arguments
static void Main()
{
string startMenuDir = ...;
string shortcutold = ...;
// Rest of your code
}
}
Basically the Mainmethod is the entry point for a stand-alone C# program.
基本上,该Main方法是独立 C# 程序的入口点。
Of course, if your code is meant to be a plug-in for something else, you may need to just implement an interface or something similar. Either way, you'll have to have your code in members rather than just "bare".
当然,如果您的代码打算成为其他东西的插件,您可能只需要实现一个接口或类似的东西。无论哪种方式,您都必须在成员中使用您的代码,而不仅仅是“裸”。
回答by Andrey Voitenkov
Most likely this is what you intended to do:
这很可能是您打算执行的操作:
using System;
using WindowsInstaller;
namespace DataImporter
{
class Program
{
static void Main(string[] args)
{
string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcutold = Path.Combine(startMenuDir, @"Ohal\TV AMP (Windows XP Mode).lnk");
if (File.Exists(shortcutold))
File.Delete(shortcutold);
string startMenuDir = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
string shortcut = Path.Combine(startMenuDir, @"Ohal\TV AMP.lnk");
if (File.Exists(shortcut))
{
Console.WriteLine("Already installed...");
}
else
{
Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
Installer installer = (Installer)Activator.CreateInstance(type);
installer.InstallProduct(@"Y:\LibSetup\TVAMP313\TVAmp v3.13.msi");
}
}
}
}
回答by keivan kashani
Your Method must be in a Class Now This is in a namespace , You must declare a class in this Namespace then declare method in this class
你的方法现在必须在一个类中这是在一个命名空间中,你必须在这个命名空间中声明一个类然后在这个类中声明方法

