110,536
社区成员
发帖
与我相关
我的任务
分享
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