2012-09-21 43 views
3

在C#中如何知道文件已被插入到文件夾中。我需要這個在C#中。獲取函數知道在文件夾中插入文件

+3

如果您想跟蹤該文件已被某些其他應用程序添加的事實,請搜索FileSystemWatcher。但那裏有一些複雜性。例如。閱讀這個問題:http://stackoverflow.com/questions/8733816/configuring-filesystemwatcher-such-that-it-raises-created-event-only-when-the-fi –

回答

0

您可以確定是否存在指定的文件中使用存在於System.IO命名空間File類的方法:

bool System.IO.File.Exists(string path) 

例如:

using System; 
using System.IO; 

class Program 
{ 
    static void Main() 
    { 
    // See if this file exists in the SAME DIRECTORY. 
    if (File.Exists("TextFile1.txt")) 
    { 
     Console.WriteLine("The file exists."); 
    } 
    // See if this file exists in the C:\ directory. [Note the @] 
    if (File.Exists(@"C:\tidy.exe")) 
    { 
     Console.WriteLine("The file exists."); 
    } 
    // See if this file exists in the C:\ directory [Note the '\\' part] 
    bool exists = File.Exists("C:\\lost.txt"); 
    Console.WriteLine(exists); 
    } 
}