Error: the namespace cannot directly contain elements such as fields or methods

I am new to C # and it is hard for me to solve this error, can someone help me? This script should remove the unnecessary shortcut, and then install a new program if it has not already been installed.

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
source share
4 answers

Your code must be in the class, and then in the method. You cannot have code in a namespace. Something like the 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 ......... } } 
+6
source

As Habib says, you need to put the code in methods, constructors, etc. In this case, if the code you need is exactly what you want as an entry point, you just need to:

 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 standalone program in C #.

Of course, if your code is designed to connect to something else, you may just need to implement an interface or something like that. In any case, you will have to have the code in the members, and not just "naked".

+3
source

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

Your method should be in the class. Now it is in the namespace, you must declare the class in this namespace, and then declare the method in this class.

+1
source

Source: https://habr.com/ru/post/1445968/


All Articles