简体   繁体   中英

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. 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.

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). 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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