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

我是研究僧i 提交于 2020-04-06 22:25:07

问题


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


回答1:


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.




回答2:


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




回答3:


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

class FoxySearch
{
   public bool DoesFileExist(string filePath)
   {
       return File.Exists(filePath);
   }
}



回答4:


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");



回答5:


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




回答6:


You must add Reference to assembly in your project.



来源:https://stackoverflow.com/questions/12947845/c-sharp-using-system-io-not-woking-in-my-class-but-works-in-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!