C# using system.io not woking in my class but works in main

前端 未结 6 2098
后悔当初
后悔当初 2021-01-16 10:20

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

相关标签:
6条回答
  • 2021-01-16 10:58

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

    0 讨论(0)
  • 2021-01-16 10:59

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

    class FoxySearch
    {
       public bool DoesFileExist(string filePath)
       {
           return File.Exists(filePath);
       }
    }
    
    0 讨论(0)
  • 2021-01-16 11:00

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

    0 讨论(0)
  • 2021-01-16 11:04

    You must add Reference to assembly in your project.

    0 讨论(0)
  • 2021-01-16 11:09

    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.

    0 讨论(0)
  • 2021-01-16 11:15

    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");
    
    0 讨论(0)
提交回复
热议问题