1

I am working on an issue I do not remember ever having before. I am using VS2012 C#

When i add using System.IO; to my main program everything works fine, however when I add it to my class file it will not let me use all of the methods.

using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace FoxySearch
{    
    class FoxySearch
    {
         File.    <<<<----- here i want to add File.Exists("Blablalba")
    }
}

For some reason it wont let me add it. As soon as I add the period the intellisense closes and shows no options.When I then type it out myself it shows red and says,

System.IO.File.Exists(string) is a method but is used like a type

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
badtoy1986
  • 43
  • 6

6 Answers6

6

You haven't really given enough code to say for sure, but it sounds like you're probably trying to write "normal code" directly in a class declaration, instead of in a method or property declaration.

Classes can only include declarations - method declarations, field declarations etc. You can't write:

class Foo
{
    int i = 10; 
    Console.WriteLine(i);
}

etc. The first line is valid as it's a variable declaration - the second isn't, as it's just a method call. If you move the code into a method, then it's fine:

class Foo
{
    public void Bar()
    {
        int i = 10; 
        Console.WriteLine(i);
    }
}

Additionally, I'd suggest that you revisit your naming - using the same name for a class and a namespace is a bad idea.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    @user1208410 Don't blame yourself that strong))) But accept the answer, if this is the case – horgh Oct 18 '12 at 06:02
1

You need to put it inside a function or sub, property and so forth.

1

You need to put the code in a method, for example:

class FoxySearch
{
   public bool DoesFileExist(string filePath)
   {
       return File.Exists(filePath);
   }
}
Mario S
  • 11,715
  • 24
  • 39
  • 47
0

u have written in class, u cant write there. & also file.Exists() returns boolean value. u have to write something like this:

boolean a= File.Exists("bla");
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

You use File.Exists() in class not in method it is a problem.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
0

You must add Reference to assembly in your project.

Sashko
  • 9
  • 4