热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

判断文件是否正在使用

发布时间:2010年06月22日点击数: 佚名
  1. public bool IsFileInUse(string fileName) 
  2.        { 
  3.            bool inUse = true
  4.            if (File.Exists(fileName)) 
  5.            { 
  6.                FileStream fs = null
  7.                try 
  8.                { 
  9.                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,FileShare.None); 
  10.                    if (fs.CanWrite) 
  11.                    { 
  12.                        inUse = false
  13.                    } 
  14.                    else 
  15.                        inUse = true
  16.                    inUse = false
  17.                } 
  18.                catch 
  19.                { 
  20.                    // exception.... 
  21.                } 
  22.                finally 
  23.                { 
  24.                   if (fs != null
  25.  
  26.                        fs.Close(); 
  27.                } 
  28.               return inUse;//by yl  true表示正在使用,false没有使用 
  29.  
  30.            } 
  31.            else 
  32.            { 
  33.               return false;//文件不存在,肯定没有被使用 
  34.            } 
  35.  
  36.        } 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作