c#DirectoryInfo,GetFiles

Nat*_*Pet 1 c#

我有以下代码,工作正常.我想要做的(一次性)是检查目录是否存在,如果是这样,我想检查文件夹中是否存在文件.如果是,则返回Y,否则返回N:

string s = new DirectoryInfo("C:\\EXP_Reports\\36000").Exists
   ? new DirectoryInfo("C:\\EXP_Reports\\36000").GetFiles("EXP Report #36001.pdf")
       .Any() ? "Y" : "N"
   : "N";
Run Code Online (Sandbox Code Playgroud)

我想知道上述代码是否可以进一步优化.请注意,我想在一份声明中这样做.

Ric*_*der 7

为什么不简单地使用File.Exists.

bool q = File.Exists(@"C:\EXP_Reports\36000\EXP Report #36001.pdf");
Run Code Online (Sandbox Code Playgroud)

请参阅http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

我还将代码更改为使用bool而不是string包含Y或N.

此外,使用逐字字符串文字@"..."读取更好.