5

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");
}
user1823287
  • 51
  • 1
  • 1
  • 2
  • Please, write what kind of troubles do you have. We can't help you when question not defined – vadim Nov 14 '12 at 10:16

4 Answers4

6

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


    }
}
Habib
  • 219,104
  • 29
  • 407
  • 436
3

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:

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.

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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");
            }
        }
    }
}
  • Error 1 Program 'c:\Users\laptop.test\Documents\Visual Studio 2012\Projects\Project1\Project1\obj\Debug\Project1.exe' does not contain a static 'Main' method suitable for an entry point c:\users\laptop.test\documents\visual studio 2012\Projects\Project1\Project1\CSC Project1 Thanks, Can you make sense of this error? – user1823287 Nov 14 '12 at 11:55
1

Your Method must be in a Class Now This is in a namespace , You must declare a class in this Namespace then declare method in this class

keivan kashani
  • 1,263
  • 14
  • 15