Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Program
    {
        static void Main()
        {
            // See if this file exists in the same directory.
            if (File.Exists(@"D:\Smartkey_test\METADATA\Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml"))
            {
                Console.WriteLine("The file exists.");
            }
            // See if this file exists in the C:\ directory. [Note the @]
            if (File.Exists(Path.GetFileNameWithoutExtension(@"D:\Smartkey_test\METADATA\Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml")))
            {
                Console.WriteLine("The file exists.");
            }

            FileInfo fi = new FileInfo("D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml");

            string finalpath = Path.Combine("D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml");
           // string aPath = "D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml";
            bool existsvalue= File.Exists(finalpath);
            Console.WriteLine(existsvalue);

            // See if this file exists in the C:\ directory [Note the '\\' part]
            bool exists = File.Exists("C:\\lost.txt");
            Console.WriteLine(exists);
            Console.ReadLine();
        }
    }




every time i got the value is false. can you please anyone help me regarding this.

I have filenames like

Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml


i need to check weather the given folder that this file are not.
File.Exists
returns always false.


Please find the below link there i was found the below lines


The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.



https://stackoverflow.com/questions/18266637/why-does-system-io-file-existsstring-path-return-false


What I have tried:

File.Exists(filename) is not working properly my file name contain the spaces



<pre lang="c#">



class Program
    {
        static void Main()
        {
            // See if this file exists in the same directory.
            if (File.Exists(@"D:\Smartkey_test\METADATA\Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml"))
            {
                Console.WriteLine("The file exists.");
            }
            // See if this file exists in the C:\ directory. [Note the @]
            if (File.Exists(Path.GetFileNameWithoutExtension(@"D:\Smartkey_test\METADATA\Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml")))
            {
                Console.WriteLine("The file exists.");
            }

            FileInfo fi = new FileInfo("D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml");

            string finalpath = Path.Combine("D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml");
           // string aPath = "D:\\Smartkey_test\\METADATA\\Accounting  Voucher Display.pdf 196910351_0_03192019144415.xml";
            bool existsvalue= File.Exists(finalpath);
            Console.WriteLine(existsvalue);

            // See if this file exists in the C:\ directory [Note the '\\' part]
            bool exists = File.Exists("C:\\lost.txt");
            Console.WriteLine(exists);
            Console.ReadLine();
        }
    }
Posted
Updated 25-Mar-19 1:29am
Comments
Richard MacCutchan 25-Mar-19 7:38am    
You appear to have different numbers of spaces in your file names.

1 solution

Works for me:
C#
string path = @"D:\Test Data\Accounting Voucher  Display.pdf 196910351_0_03192019144415.xml";
if (File.Exists(path)) Console.WriteLine("Exists");
else Console.WriteLine("Does not exist");
I get Exists every time.
The only difference I can see is the full path - I known my D: drive contains a folder called "Test Data", and that contains a file called "Accounting Voucher Display.pdf 196910351_0_03192019144415.xml".

Check the rest of your path.
If it still looks right, do this:
C#
string[] files = Directory.GetFiles(@"D:\", "*.xml", SearchOption.AllDirectories);
And use the debugger to see exactly what files it can find.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900