C#使用system.io在我的课堂上不起作用,但在main中起作用
我正在研究一个我不记得以前有过的问题。 我正在使用VSC# 当我使用C#使用system.io在我的课堂上不起作用,但在main中起作用,c#,class,.net,using-directives,C#,Class,.net,Using Directives,我正在研究一个我不记得以前有过的问题。 我正在使用VSC# 当我使用System.IO添加时;对于我的主程序来说,一切都很好,但是当我将它添加到我的类文件中时,它不允许我使用所有的方法 using System; using System.Collections.Generic; using System.IO; using System.Data.SQLite; using System.Linq; using System.Text; using System.Threading.Tasks
System.IO添加时
;对于我的主程序来说,一切都很好,但是当我将它添加到我的类文件中时,它不允许我使用所有的方法
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")
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Data.SQLite;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间FoxySearch
{
FoxySearch类
{
文件。您实际上没有给出足够的代码来确定,但听起来您可能试图直接在类声明中编写“普通代码”,而不是在方法或属性声明中
类只能包含声明-方法声明、字段声明等。您不能编写:
class Foo
{
int i = 10;
Console.WriteLine(i);
}
等等。第一行是有效的,因为它是一个变量声明,而第二行不是有效的,因为它只是一个方法调用。如果您将代码移动到一个方法中,则可以:
class Foo
{
public void Bar()
{
int i = 10;
Console.WriteLine(i);
}
}
此外,我建议您重新考虑您的命名-对类和名称空间使用相同的名称是一个。您需要将其放在函数或子函数、属性等中。您已经在类中编写了,您不能在类中写入。&另外,file.Exists()返回布尔值。您必须这样编写:
boolean a= File.Exists("bla");
您在类中使用File.Exists(),而不是在方法中使用。这是一个问题。您需要将代码放入方法中,例如:
class FoxySearch
{
public bool DoesFileExist(string filePath)
{
return File.Exists(filePath);
}
}
您必须在项目中添加对程序集的引用。您不能在类定义中写入它。@user1208410不要自责太强)),但如果是这样,请接受答案