如何检查文件是否存在 [英] How Do I Check If A File Exists

查看:69
本文介绍了如何检查文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我正在使用Windows 8并运行Visual Studio 2013.我想测试一个文件是否存在于名为temp的文件夹中C驱动器我称之为test.txt的文件:下面是代码:



Hi all.

I am using Windows 8 and running Visual Studio 2013. I wanted to test if a file exists in a folder called "temp" in the "C" drive The file I called "test.txt": Below is the code:

static void Main(string[] args)
        {

            string testFile = @"C:\temp\test.txt";

            if (File.Exists(testFile))
            {
                Console.WriteLine("Eureka. File found");

                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("File is not found..");
                Console.ReadLine();
            }
        }





但是,尽管这个文件存在(我在名为的文件夹中盯着它temp在驱动器C !!)中,它继续跳转到else语句。请帮助我对这个看似简单的代码失去理智。



But, despite that this file exists (I am staring at it in the folder called temp in drive C!!), It keeps on jumping to the else statement. Please assist for I am losing my mind over this seemingly easy code.

推荐答案

代码看起来应该有效:事实上,如果我在这里检查它确实。

所以...检查你的文件。打开Windows资源管理器,查看文件夹C:\ temp。

右键单击文件并选择属性:(这应该在Win8中工作,但我无法检查)

选择详细信息选项卡并查看名称:是test.txt吗?或者test.txt.txt?



请记住,默认情况下Explorer会隐藏已知文件的扩展名,所以如果你能看到.txt扩展名,那么可能不是实际的文件扩展名 - 您的代码必须引用确切的文件名,因为它不适用于隐藏扩展名。
The code looks like it should work: and indeed if I check it here it does.
So...check your file. Open Windows Explorer, all look at the folder "C:\temp".
Right click the file and select Properties: (this should work in Win8, but I can't check)
Select the "Details" tab and look at the Name: is it "test.txt"? Or "test.txt.txt"?

Remember that Explorer hides the extensions for known files by default, so if you can see the ".txt" extension, it probably isn't the actual file extension - your code must refer to the exact file name, because it does not work with "hidden" extensions.


这篇关于如何检查文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆