Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam try to check that the file is in use or not in c#
i want to check the simple text file

What I have tried:

C#
public static bool IsFileLocked(string filePath) 
        {
            bool lockStatus = false;
            try
            {
                using (FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) 
                {
                    // File/Stream manipulating code here

                    lockStatus = !fileStream.CanWrite;
                    
                }
            }
            catch
            {
                //check here why it failed and ask user to retry if the file is in use.
                lockStatus = true;
            }
            return lockStatus;
        }
Posted
Updated 29-Apr-16 1:20am
v2

C#
try
{
using(FileStream fs = File.OpenWrite(strFileName)
{
if(fs == null)
return;
}
}
catch(UnauthorizedAccessException e)
{
//Here you know that the file is open by another app
}




or Else, Try this!!!


C#
public bool FileIsLocked(string strFullFileName) {
        bool blnReturn = false;
        System.IO.FileStream fs;
        try {
            fs = System.IO.File.Open(strFullFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None);
            fs.Close();
        }
        catch (System.IO.IOException ex) {
            blnReturn = true;
        }
        return blnReturn;
    }
 
Share this answer
 
v2
Comments
CHill60 29-Apr-16 11:01am    
Which one of your two answers is a reader meant to believe is the solution?
You can use the Improve solution link to update a solution. Posting more than one solution to a single question is confusing for everyone
Santosh Kokatnur 30-Apr-16 0:59am    
Sir, It couldn't be happen next time!
Try this!

C#
protected virtual bool IsFileLocked(FileInfo file)
{
    FileStream stream = null;

    try
    {
        stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
    }
    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;
    }
    finally
    {
        if (stream != null)
            stream.Close();
    }

    //file is not locked
    return false;
}
 
Share this answer
 
Comments
srilekhamenon 29-Apr-16 7:04am    
this is not working in case of simple text file
Santosh Kokatnur 29-Apr-16 7:08am    
What you want to achieve , Clear it first!
srilekhamenon 29-Apr-16 7:12am    
iam try to move files from one directory to other. if any of the files in the directory is opened by user it will not move properly
so iam checking each files in the directory wheather it is opened or not looping all the files in directory
Santosh Kokatnur 29-Apr-16 7:15am    
Dear, Go through my Second solution!
Philippe Mori 29-Apr-16 21:34pm    
State can change between the time you check and the time you move a file so it might be preferable to display a message asking the user what to do...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900