求C#判断文件名是否存在函数

Flammable_ice 2016-06-15 08:50:25
1.求file.Exists内部实现源码

2.求判断含有空格的文件路径名是否存在源码?
两者选其一
...全文
325 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
我叫小菜菜 2016-06-15
  • 打赏
  • 举报
回复
引用 5 楼 z84616995z 的回复:
[quote=引用 3 楼 zhi_ai_yaya 的回复:] 其实看这源码并没有什么实用意义。 直接使用文件名是否存在并遍历是否存在空格就行了。当然如果类库存在模糊匹配函数可以直接使用。
确实 所以我想问:有没有办法实现 判断文件是否存在时允许文件名包含空格的方法[/quote]
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"F:\主机  型号.txt";
            bool rt = File.Exists(s);
            Console.WriteLine("{0} is {1}", s, rt ? "存在" : "不存在");
           Console.ReadKey();   
        }
    }
//output

F:\主机  型号.txt is 存在
搜索指定目录下文件名包含空格的文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = @"F:\主机  型号.txt";
           // bool rt = File.Exists(s);
           // Console.WriteLine("{0} is {1}", s, rt ? "存在" : "不存在");
           //Console.ReadKey();
           string path = @"F:\";
           string filter = "* *"; //使用通配符,两个*号之间包含一个空格,即可搜索包含空格的文件名

           PrintFileSystemEntries(path, filter);

           Console.ReadKey();
 
        }

        static void PrintFileSystemEntries(string path, string pattern)
        {
            try
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern);

                foreach (string str in directoryEntries)
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException)
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException)
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException)
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " +
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                System.Console.WriteLine("The path encapsulated in the " +
                    "Directory object does not exist.");
            }
        }
    }
}
https://msdn.microsoft.com/zh-cn/library/d3eayw32%28v=vs.110%29.aspx
xuzuning 2016-06-15
  • 打赏
  • 举报
回复

Console.WriteLine(File.Exists("123 456.txt")); //True

有什么问题吗?
Flammable_ice 2016-06-15
  • 打赏
  • 举报
回复
引用 3 楼 zhi_ai_yaya 的回复:
其实看这源码并没有什么实用意义。 直接使用文件名是否存在并遍历是否存在空格就行了。当然如果类库存在模糊匹配函数可以直接使用。
确实 所以我想问:有没有办法实现 判断文件是否存在时允许文件名包含空格的方法
老许要老婆么 2016-06-15
  • 打赏
  • 举报
回复
懒死你
我叫小菜菜 2016-06-15
  • 打赏
  • 举报
回复
其实看这源码并没有什么实用意义。 直接使用文件名是否存在并遍历是否存在空格就行了。当然如果类库存在模糊匹配函数可以直接使用。
江南小鱼 2016-06-15
  • 打赏
  • 举报
回复
File.Exists源码,你反编译下.net类库的对应dll呗

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧