回答
if (System.IO.File.Exists("pathtofile"))
//it exist
else
//it does not exist
编辑我的回答这个问题的后评论:
我复制的代码,改变了退出功能,这应该工作
string type = Path.GetExtension(filepath);
string path = @"image/" + type + ".png";
//if(System.IO.File.Exists(path)) I forgot to use the full path
if (System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), path)))
{ return path; }
else
{ return @"image/other.png"; }
这的确会工作时,你的应用程序部署
另一种方法是使用'FileInfo',如果您还需要获取时间戳和其他基本信息。 – 2010-08-10 07:39:53
@Steven:这是正确的,如果你想要的文件的信息,但File.Exists有更好的性能,如果你只需要知道是否存在 – 2010-08-10 07:50:52
是的,这就是为什么我建议它作为替代如果你也要去需要额外的信息,而不是一般的替代品。 – 2010-08-10 08:40:45
你可以使用
string[] filenames = Directory.GetFiles(path);
得到的文件列表中的文件夹中,然后遍历它们,直到你找到你想找的(或不)
,或者你可以尝试在try catch块打开该文件,如果你得到它意味着该文件不存在的异常。
这些都不是好主意。 – 2010-08-10 07:39:23
效率不如'File.Exists'或'FileInfo.Exists'方法。 – tdammers 2010-08-10 07:44:49
效率真的很低?你会以懒惰的评估方式使用它,或者在启动时获取文件并保留列表。 – 2010-08-12 08:45:20
使用File.Exists(Path Here)
如果您使用临时路径使用Path.GetTempPath()
编辑:对不起,相同的答案以上!
这个问题有点不清楚,但我得到的印象是,你在 之后exe已经安装的路径?
class Program
{
static Dictionary<string, string> typeImages = null;
static string GetImagePath(string type)
{
if (typeImages == null)
{
typeImages = new Dictionary<string, string>();
string appPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = Path.Combine(appPath, @"image/");
foreach (string file in Directory.GetFiles(path))
{
typeImages.Add(Path.GetFileNameWithoutExtension(file).ToUpper(), Path.GetFullPath(file));
}
}
if (typeImages.ContainsKey(type))
return typeImages[type];
else
return typeImages["OTHER"];
}
static void Main(string[] args)
{
Console.WriteLine("File for XLS="+GetImagePath("XLS"));
Console.WriteLine("File for ZZZ=" + GetImagePath("ZZZ"));
Console.ReadKey();
}
}
这将给你一个图像文件夹,将在任何地方安装exe。 在开发环境中,您必须在应用程序路径下调试并释放图像目录,因为这是VS放置exe的位置。
- 1. 检查包/项目中是否存在文件/文件夹
- 2. 如何检查文件夹中是否已存在文件
- 3. 如何检测文件夹是否是github存储库
- 4. AngularJS - 检查文件夹是否存在
- 5. 检查IMAP是否存在文件夹
- 6. PHP - 检查一个文件夹中是否存在文件
- 7. 检查文件夹中是否存在文件actionScript 3
- 8. Flex 4.5.1检查资产文件夹中是否存在文件
- 9. git是否在项目树中创建文件/文件夹?
- 10. 如何检查Cocoa&Objective-C中是否存在文件夹?
- 11. 无法检测文件是否存在
- 12. 检测文件是否存在
- 13. 如何通过FTP检查远程文件夹中是否存在文件?
- 14. 使用AngularJS如何检查文件夹中是否存在PDF文件?
- 15. 使用Jekyll如何检查文件夹中是否存在文件?
- 16. 如何检查文件是否存在于Android上的/ data文件夹中
- 17. Sharepoint:通过webservices检查项目是否存在于sharepoint list子文件夹中?
- 18. 如何检查文件是否存在?
- 19. 如何检查文件是否存在?
- 20. 如何检测Firebase存储文件是否存在?
- 21. 如何检查文件是否存在于war文件中?
- 22. 如何检查chm文件中是否存在子文件?
- 23. 如何检查目录是文件还是文件夹?
- 24. WebDav如何检查文件夹是否存在?
- 25. 如何检查一个文件夹是否存在使用PHP
- 26. Adobe Air如何检查文件夹是否存在?
- 27. 如何检索项目文件夹下的XML文件
- 28. 如何检索项目文件夹内的pdf文件?
- 29. 如何找出文件夹是否存在以及如何创建文件夹?
- 30. 使用Zend邮件检查邮件文件夹是否存在
你能指定什么时候你需要检测这些?我们在编译之前还是编译的程序正在运行? – 2010-08-10 07:38:05
我有一个项目列表视图,数据绑定到我的本地文件夹Z上的文件列表,包括各种文件如.doc,.xls等。 在我的项目(解决方案)文件中,我有一个文件夹图像文件的收集,即doc.png,xls.png等 我现在要做的是循环文件夹Z中的文件,检测文件类型,并尝试返回如下: string type = Path。 GetExtension(文件路径); string path = @“image /”+ type +“.png”; (存在(路径)) { return path; } else { return @“image/other.png”; } 因为这些文件位于我的解决方案文件夹中,所以我不确定它会在部署之后起作用。 – VHanded 2010-08-12 05:57:57