C# 检查文件是否正在使用

来源:互联网 发布:淘宝网表带 编辑:程序博客网 时间:2024/06/05 04:19
/// <summary>        /// 检查文件是否正在使用        /// </summary>        /// <param name="fileName"></param>        /// <returns></returns>        public static bool IsFileInUse(string fileName)        {            try            {                bool inUse = true;                FileStream fs = null;                try                {                    fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,                    FileShare.None);                    inUse = false;                }                catch                {                }                finally                {                    if (fs != null)                        fs.Close();                }                return inUse;//true表示正在使用,false没有使用            }            catch { }            return false;        }

原创粉丝点击