5

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

4 回答 4

6

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

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


    }
}
于 2012-11-14T09:59:13.093 回答
3

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

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

于 2012-11-14T10:01:12.000 回答
1

这很可能是您打算做的事情:

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");
            }
        }
    }
}
于 2012-11-14T10:02:18.813 回答
1

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

于 2017-05-24T07:58:32.040 回答