【问题标题】:Error: A namespace cannot directly contain members such as fields or methods [duplicate]错误:命名空间不能直接包含字段或方法等成员[重复]
【发布时间】:2012-11-14 09:58:10
【问题描述】:

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

【问题讨论】:

  • 拜托,你有什么烦恼。当问题未定义时,我们无法为您提供帮助

标签: c#


【解决方案1】:

您的代码应该在一个类中,然后是一个方法。您不能在命名空间下拥有代码。类似于以下的东西。

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


    }
}

【讨论】:

    【解决方案2】:

    正如 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# 程序的入口点。

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

    【讨论】:

      【解决方案3】:

      这很可能是您打算做的:

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

      【讨论】:

      • 错误 1 ​​程序 'c:\Users\laptop.test\Documents\Visual Studio 2012\Projects\Project1\Project1\obj\Debug\Project1.exe' 不包含静态 'Main' 方法适合入口点 c:\users\laptop.test\documents\visual studio 2012\Projects\Project1\Project1\CSC Project1 谢谢,你能理解这个错误吗?
      【解决方案4】:

      你的方法现在必须在一个类中这是在一个命名空间中,你必须在这个命名空间中声明一个类,然后在这个类中声明方法

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多