Check whether a file is in use or not

/// <summary> 
/// This function checks whether the file is in use or not. 
/// </summary> 
/// <param name="filename">File Name</param> 
/// <returns>Return True if file in use else false</returns> 
public static bool IsFileInUse(string filename) 
{ 
    bool locked = false; 
    FileStream fs = null; 
    try 
    { 
        fs = 
             File.Open(filename, FileMode.OpenOrCreate, 
             FileAccess.ReadWrite, FileShare.None); 
    } 
    catch (IOException ) 
    { 
        locked = true; 
    } 
    finally 
    { 
        if (fs != null) 
        { 
            fs.Close(); 
        } 
    } 
    return locked; 
} 
posted @ 2012-03-19 11:02  unstable  阅读(112)  评论(0编辑  收藏  举报