简体   繁体   English

错误:名称空间不能直接包含诸如字段或方法之类的成员

[英]Error: A namespace cannot directly contain members such as fields or methods

I'm new to C# and I'm having trouble solving this error can anyone help me please? 我是C#的新手,无法解决此错误,有人可以帮助我吗? This script is to delete an un-needed shortcut then install a new program if it hasn't been installed already. 该脚本用于删除不需要的快捷方式,然后安装尚未安装的新程序。

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

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


    }
}

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 Main method 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". 无论哪种方式,您都必须将代码包含在成员中,而不仅仅是“裸露”。

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

您的Method现在必须在类中这是在名称空间中,您必须在此Namespace中声明一个类,然后在该类中声明方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命名空间不能直接包含成员,如字段或方法? - A namespace cannot directly contain members such as fields or methods? 错误4名称空间不能直接包含成员,例如字段或方法 - Error 4 A namespace cannot directly contain members such as fields or methods C# 错误:“命名空间不能直接包含字段或方法等成员” - C# Error: "A namespace cannot directly contain members such as fields or methods" Unity报错:命名空间不能直接包含字段或方法等成员 - Unity error message: A namespace cannot directly contain members such as fields or methods c#名称空间不能直接包含成员,例如字段或方法 - c# a namespace cannot directly contain members such as fields or methods 命名空间不能直接包含字段或方法等成员。 - A namespace cannot directly contain members such as fields or methods. “名称空间不能直接包含诸如字段或方法之类的成员” File:具有上下文的控制器 - “A namespace cannot directly contain members such as fields or methods” File: controller with context C#名称空间不能直接包含诸如void的字段或方法之类的成员 - C# A namespace cannot directly contain members such as fields or methods for void 错误:名称空间不直接包含诸如字段或方法之类的成员 - Error :A namespace does not directly contain members such as fields or methods 错误 CS0116:命名空间不能直接包含成员,例如字段或方法 - Error CS0116: A namespace cannot directly contain members such as fields or methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM