Is File In Use - CSharp File IO

CSharp examples for File IO:File Command

Description

Is File In Use

Demo Code


using System.IO;//from w w  w . ja  v  a  2s. c om
using System;

public class Main{
        public static bool IsInUse(this FileInfo file)
        {
            FileStream stream = null;

            try
            {
                stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (Exception)
            {
                //the file is unavailable because it is:
                //still being written to
                //or being processed by another thread
                //or does not exist (has already been processed)
                return true;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
            return false;
        }
}

Related Tutorials