检查文件是否存在错误
所以,我看到了另一个问题的答案说,这应该工作:检查文件是否存在错误
using System.IO;
if (File.Exists(Path))
{
Action();
}
然而,当我这样做,我得到这些错误:
'System.IO' is a 'namespace', which is not valid in the given context
The Name 'File' does not exist in the current context
什么我做错了吗?
无论是在你的文件的顶部添加using System.IO;
或者
使用它像
if (System.IO.File.Exists(Path))
{
//do whatever
}
你需要把using System.IO;
在外部类文件的顶部。
我能问这是为什么不接受的答案?罗比在@Shyju之前回答,但你却接受了他的回答? – 2012-04-12 19:27:21
也许它因为@Shyju提供没有一个'using'的额外选择?我没有真正困扰,但感谢有我的角落:) – Robbie 2012-04-12 19:48:17
在你的文件的顶部:
using System.IO; <--
namespace Application1
{
它不一定需要是命名空间声明之外,但它必须是类声明 – Davy8 2012-04-09 01:37:40
@ Davy8哦之外,我没明白这一点,谢谢! – 2012-04-10 08:46:08
很难告诉你在做什么LY,但它看起来像你可能需要一些帮助,您的语句的顺序。 using
语句出现在cs文件的开始,你的逻辑将需要出现在一个类中的方法。
下面是它可以使用一个控制台应用程序来完成:
using System.IO;
public class Program
{
public static void Main(string[] args)
{
string path = @"c:\temp\file.txt";
if (File.Exists(path))
{
Action();
}
}
}
你也可能想看看ReSharper的,一个工具(不是免费的),有助于编写更干净的代码,并显示该类型的异常当你输入代码时是正确的。 – Mathieu 2012-04-09 02:36:06