繁体   English   中英

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

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

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

您的代码应在类中,然后在方法中。 您不能在名称空间下使用代码。 像下面这样。

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


    }
}

正如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
    }
}

基本上, Main方法是独立C#程序的入口点。

当然,如果您的代码打算用作其他插件,则可能只需要实现一个接口或类似的东西。 无论哪种方式,您都必须将代码包含在成员中,而不仅仅是“裸露”。

这很可能是您打算要做的:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM