Check whether file is in use using C# in .Net

Waghmare
 
on Sep 28, 2021 10:56 PM
461 Views

Hi Team,

Is there a way to check if a file is in use to avoid error of file is being used by another process.

using System.Configuration;
using System.IO;

namespace FileConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Operation();
        }

        protected virtual bool IsFileLocked(FileInfo file)
        {
            try
            {
                using (FileStream stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    stream.Close();
                }
            }
            catch (IOException)
            {
                //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;
            }

            //file is not locked
            return false;
        }

        public static void Operation()
        {
            string FileSource = ConfigurationManager.AppSettings["FileSource"];
            string FileDestination = ConfigurationManager.AppSettings["FileDestination"];

            if ((Directory.Exists(FileSource)) && (Directory.Exists(FileDestination)))
            {
                //copy files from Source Fake Files
                string[] files_Source = Directory.GetFiles(FileSource);
                //copy files from destination - Org files 
                string[] files_Destination = Directory.GetFiles(FileDestination);

                string FileName_source = "", FileName_dest = "", File_Deletion = "";

                foreach (string source in files_Source)  //  Fake files_Destination 
                {
                    FileName_source = System.IO.Path.GetFileName(source);

                    foreach (string dest in files_Destination)
                    {
                        FileName_dest = System.IO.Path.GetFileName(dest);

                        if (FileName_source.ToUpper() == FileName_dest.ToUpper())
                        {

                            File_Deletion = System.IO.Path.Combine(FileDestination, FileName_dest);

                            if (File.Exists(File_Deletion))
                            {
                                File.Delete(File_Deletion);
                            }

                            FileDestination = System.IO.Path.Combine(FileDestination, FileName_source);
                            FileName_source = System.IO.Path.Combine(FileSource, FileName_source);

                            System.IO.File.Copy(FileName_source, FileDestination, true);

                            if (File.Exists(FileDestination))
                            {
                                File.SetLastWriteTime(FileDestination, DateTime.Now);
                            }

                            FileDestination = "";
                            FileDestination = ConfigurationManager.AppSettings["FileDestination"];
                        }
                    }
                }
            }
            else
            {
            }
        }
    }
}

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 29, 2021 06:06 AM
Waghmare says:
        protected virtual bool IsFileLocked(FileInfo file)
        {
            try
            {
                using (FileStream stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    stream.Close();
                }
            }
            catch (IOException)
            {
                //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;
            }
 
            //file is not locked
            return false;
        }

You had already implemented. What is the issue?