简体   繁体   English

c#File.Exists总是使用value属性返回false

[英]c# File.Exists always return false using value property

During debugging I found something annoying. 在调试过程中,我发现有些烦人。 I have a file existing on my drive, I'm wondering why if I use the file exists function it always returns false, using property value. 我的驱动器上有一个文件,我想知道为什么如果我使用存在文件功能,它总是使用属性值返回false。 I try also in Immediate Window here are the results 我也在即时窗口中尝试这里是结果

ACGateLoginSystem.MAP_PATH == @"‪D:\Capture001.png" | true

?File.Exists(ACGateLoginSystem.MAP_PATH) | false

?File.Exists("D:\\Capture001.png") | true

I'm using windows 10 latest build, and visual studio 2017. 我正在使用Windows 10最新版本和Visual Studio 2017。

Following is working for me. 以下为我工作。

namespace ConsoleApplication1
{
    class LoginSystem
    {
        public string MAP_PATH { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            LoginSystem ACGateLoginSystem = new LoginSystem();
            ACGateLoginSystem.MAP_PATH = @"D:\1.png";

            if (File.Exists(ACGateLoginSystem.MAP_PATH))
                Console.WriteLine("File Exists");

            if (File.Exists("D:\\1.png"))
                Console.WriteLine("File Exists - with direct path");

            Console.ReadLine();
        }
    }
}

Output: 输出: 在此处输入图片说明

The backslash character \\ is a special character in C# (and any C-like language). 反斜杠字符\\是C#(和任何类似C的语言)中的特殊字符。 It is used in conjunction with a second one to define special character. 它与第二个字符结合使用以定义特殊字符。 Thus, this would work: 因此,这将起作用:

File.Exists("D:\\Capture001.png")

and this should work 这应该工作

File.Exists(@"D:\Capture001.png")

and this won't work 这是行不通的

File.Exists("D:\Capture001.png")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM