如果该文件存在,则为 true;如果该文件不存在或如果该文件是目录,则为 false。using System; using System.IO;public class ExistsTest { public static void Main() { string neFile = "nonexistentfile"; // open an existing file, or create a new one FileInfo fi = new FileInfo(neFile); DetermineExists(fi, neFile); neFile = "a.txt"; // create a file, so it exists fi = new FileInfo(neFile); FileStream fs = fi.Create(); DetermineExists(fi, neFile); // close the file so it can be deleted fs.Close(); // delete the file try { fi.Delete(); Console.WriteLine("The file '{0}' was deleted successfully", fi.Name); } catch (Exception e) { Console.WriteLine(e.ToString()); } } private static void DetermineExists( FileInfo fi, string fileName ) { // figure out if the file exists or not if (fi.Exists) Console.WriteLine("The file '{0}' exists in the specified directory", fileName); else Console.WriteLine("The file '{0}' does not exist in the specified directory", fileName); } }
if(theFile.Exists)
{
MessageBox.Show("存在");
}
DirectoryInof[] folderArray = theFolder.GetDirectories();
foreach( DirectoryInfo folder in folderArray)
{
FileInfo[] fileArray = folder.GetFiles();
foreach( FileInfo file in fileArray )
{
if( fileInfo.Name == "a.txt" )
MessageBox.Show( "Exist");
break;
}
}
DirectoryInof[] folderArray = theFolder.GetDirectories();
foreach( DirectoryInfo folder in folderArray)
{
String path = folder.Name + "\a.txt";
FileInfo file = new FileInfo(path);
if(file.Exists)
MessageBox.Show( "Exist");
break;
}
}
DirectoryInof[] folderArray = theFolder.GetDirectories();
foreach( DirectoryInfo folder in folderArray)
{
String path = folder.Name + "\a.txt";
FileInfo file = new FileInfo(path);
if(file.Exists)
MessageBox.Show( "Exist");
break;
}
}
MessageBox.Show("该文件存在");
DirectoryInof[] folderArray = Folder.GetDirectories();
foreach( DirectoryInfo folder in folderArray)
{
String p = folder.Name + "\\a.txt";
FileInfo file = new FileInfo(p);
if(file.Exists)
MessageBox.Show( "存在!");
break;
}
}
-------------------
FileInfo aFile = new FileInfo(@"c:\glf\mynote.txt");
if(aFile.Exists)
{
MessageBox.Show("存在");
}
else
{
MessageBox.Show("不存在");
}
*************
请各位看清~~
using System.IO;public class ExistsTest {
public static void Main() { string neFile = "nonexistentfile"; // open an existing file, or create a new one
FileInfo fi = new FileInfo(neFile); DetermineExists(fi, neFile); neFile = "a.txt"; // create a file, so it exists
fi = new FileInfo(neFile);
FileStream fs = fi.Create(); DetermineExists(fi, neFile); // close the file so it can be deleted
fs.Close(); // delete the file
try {
fi.Delete();
Console.WriteLine("The file '{0}' was deleted successfully", fi.Name);
} catch (Exception e) {
Console.WriteLine(e.ToString());
}
} private static void DetermineExists( FileInfo fi, string fileName ) { // figure out if the file exists or not
if (fi.Exists)
Console.WriteLine("The file '{0}' exists in the specified directory", fileName);
else
Console.WriteLine("The file '{0}' does not exist in the specified directory", fileName);
}
}