1.求file.Exists内部实现源码
或
2.求判断含有空格的文件路径名能否存在源码?
两者选其一
或
2.求判断含有空格的文件路径名能否存在源码?
两者选其一
解决方案
2
File.Exists源码,你反编译下.net类库的对应dll呗
8
2
其实看这源码并没有什么实用意义。
直接使用文件名能否存在并遍历能否存在空格就行了。当然假如类库存在模糊匹配函数可以直接使用。
直接使用文件名能否存在并遍历能否存在空格就行了。当然假如类库存在模糊匹配函数可以直接使用。
1
10
Console.WriteLine(File.Exists(“123 456.txt”)); //True
有什么问题吗?
17
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