相同文件只能一个进程读取

       string straa = ReadFile(file.FullName, FileAccess.Read, FileShare.None);

 

       private string ReadFile(string strFilePath,FileAccess fileAccess, FileShare fileShare)
        {
            try
            {
                FileStream fs = new FileStream(strFilePath, FileMode.Open, fileAccess, fileShare);
                var buffer = new byte[fs.Length];
                fs.Position = 0;
                fs.Read(buffer, 0, buffer.Length);
                return Encoding.UTF8.GetString(buffer);
            }
            catch (System.IO.IOException ex)
            {
                ///正由另一进程使用,因此该进程无法访问该文件
                throw (ex);
            }
            catch (Exception ex)
            {
                throw (ex);
            } 
        }

posted @ 2012-12-13 14:12  星火卓越  阅读(181)  评论(0编辑  收藏  举报