错误:名称空间不能直接包含诸如字段或方法之类的成员 [英] Error: A namespace cannot directly contain members such as fields or methods

查看:60
本文介绍了错误:名称空间不能直接包含诸如字段或方法之类的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我无法解决此错误,有人可以帮助我吗?该脚本用于删除不需要的快捷方式,然后在尚未安装新程序的情况下安装它.

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.

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


    }
}

这篇关于错误:名称空间不能直接包含诸如字段或方法之类的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆