判断文件是否正在使用

代码
 public bool IsFileInUse(string fileName)
        {
            
bool inUse = true;
            
if (File.Exists(fileName))
            {
                FileStream fs 
= null;
                
try
                {
                    fs 
= new FileStream(fileName, FileMode.Open, FileAccess.Read,FileShare.None);
                    
if (fs.CanWrite)
                    {
                        inUse 
= false;
                    }
                    
else
                        inUse 
= true;
                    inUse 
= false;
                }
                
catch
                {
                    
// exception....
                }
                
finally
                {
                   
if (fs != null)

                        fs.Close();
                }
               
return inUse;//by yl  true表示正在使用,false没有使用

            }
            
else
            {
               
return false;//文件不存在,肯定没有被使用
            }

        }
原文地址:https://www.cnblogs.com/hantianwei/p/1634253.html