File.exists(filename)无法正常工作我的文件名包含空格 [英] File.exists(filename) is not working properly my file name contain the spaces

查看:243
本文介绍了File.exists(filename)无法正常工作我的文件名包含空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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需要检查天气这个文件不是给定的文件夹。



i need to check weather the given folder that this file are not.

File.Exists

总是返回false。





请在下面找到以下链接我被发现以下行





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





我的尝试:






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();
        }
    }

推荐答案

适用于我:

Works for me:
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");

我每次都存在

唯一的区别是我能看到的是完整路径 - 我知道我的D:驱动器包含一个名为测试数据的文件夹,其中包含一个名为Accounting Voucher Display.pdf 196910351_0_03192019144415.xml的文件。



检查路径的其余部分。

如果看起来仍然正常,请执行以下操作:

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:

string[] files = Directory.GetFiles(@"D:\", "*.xml", SearchOption.AllDirectories);

并使用调试器准确查看它可以找到的文件。

And use the debugger to see exactly what files it can find.


这篇关于File.exists(filename)无法正常工作我的文件名包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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