I have the following code which works fine. What I am trying to do (in one shot) is to check if a directory exists, and, if so I would like to check if a file exists within the folder. It returns Y if it does or else it returns 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";
I am wondering if the above code can be optimized further. Please note that I would like to do it in one statement.
Why not simply use
File.Exists.See http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
I’ve also changed the code to use a
boolinstead of astringcontaining Y or N.Also, using a verbatim string literal
@"..."reads better.