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

       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);
            } 
        }

你可能感兴趣的:(文件)