none
Delete All Cookies Via Desktop App

    Question

  • Trying to test my function and in a class it works no errors but when I put it in a desktop app I get errors

    code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Clearcookie
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
        }
    
        public static void clearIECache()
        {
            ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
        }
    
        public static void ClearFolder(DirectoryInfo folder)
        {
            foreach (FileInfo file in folder.GetFiles())
            {
                file.Delete();
            }
    
            foreach (DirectoryInfo subfolder in folder.GetDirectories())
            {
                ClearFolder(subfolder);
            }
        }
    }


    Error:


    Severity


    Code


    Description


    Project


    File


    Line


    Error


    CS0116


    A namespace cannot directly contain members such as fields or methods


    Clearcookie


    c:\users\mholmes\documents\visual studio 2015\Projects\Clearcookie\Clearcookie\Form1.cs


    22

    • Edited by old_School Tuesday, April 4, 2017 3:37 PM
    Tuesday, April 4, 2017 3:36 PM

Answers

  • Brackets are wrong :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    namespace Clearcookie
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            public static void clearIECache()
            {
                ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
            }
    
            public static void ClearFolder(DirectoryInfo folder)
            {
                foreach (FileInfo file in folder.GetFiles())
                {
                    file.Delete();
                }
    
                foreach (DirectoryInfo subfolder in folder.GetDirectories())
                {
                    ClearFolder(subfolder);
                }
            }
        }
    }
    


    • Edited by Castorix31 Tuesday, April 4, 2017 4:02 PM
    • Marked as answer by old_School Tuesday, April 4, 2017 4:06 PM
    Tuesday, April 4, 2017 4:01 PM

All replies

  • Brackets are wrong :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    namespace Clearcookie
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
    
            public static void clearIECache()
            {
                ClearFolder(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
            }
    
            public static void ClearFolder(DirectoryInfo folder)
            {
                foreach (FileInfo file in folder.GetFiles())
                {
                    file.Delete();
                }
    
                foreach (DirectoryInfo subfolder in folder.GetDirectories())
                {
                    ClearFolder(subfolder);
                }
            }
        }
    }
    


    • Edited by Castorix31 Tuesday, April 4, 2017 4:02 PM
    • Marked as answer by old_School Tuesday, April 4, 2017 4:06 PM
    Tuesday, April 4, 2017 4:01 PM
  • Thanks
    Tuesday, April 4, 2017 4:06 PM