先看下面一段代码(先以共享的方式打开文件读写,然后以只读的方式打开相同文件):

FileStream fs  = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                FileStream fs2 = new FileStream(filePath, FileMode.Open, FileAccess.Read) 或者 new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

第一句成功执行,第二句呢?它抛出访问违规异常:The process cannot access the file 'c:\Odma32.log' because it is being used by another process!

查阅MSDN,无果。后经多次实验,包括使用Windows API CreateFile(...),最终发现正确的用法居然是:

FileStream fs  = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                FileStream fs2 = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

在打开fs2时,它必须制定FileShare为ReadWrite,因为fs以ReadWrite的方式打开的。

结论:FileShare不只是对随后的打开文件请求有影响,但事实是它对已经打开的文件句柄也有影响。MSDN中关于FileShare的解释不到位,应该CreateFile条目中的这一段也放进去:

You cannot request a sharing mode that conflicts with the access mode that is specified in an existing request that has an open handle. CreateFile would fail and the GetLastError function would return ERROR_SHARING_VIOLATION.

转载于:https://www.cnblogs.com/mschen/p/5353862.html

FileStream:The process cannot access the file because it is being used by another process相关推荐

  1. smbException:The process cannot access the file because it is being used by another process的错误原因

    smbException:The process cannot access the file because it is being used by another process 问题我搜了百度上 ...

  2. 处理文件时遇到:The process cannot access the file because it is being used by another process.

    没空啰嗦,直接上代码! class FileOperate {         //for security , not display the account in the app.config   ...

  3. golang错误:The process cannot access the file because it is being used by another process

    情况一,创建文件后再删除: func main() {NewConfigIni()DeleteConfigTemp() }func DeleteConfigTemp() {path := " ...

  4. the process cannot access the file because it is being used by another process

    当在IIS中修改绑定的端口号后启动时遇到如下错误,表明你的端口号已经被占用了 使用netstat -ano这个指令查看本地端口占用情况,换一个没有被占用的端口即可.  版权声明:本文为博主原创文章 ...

  5. goland编译时提示\go-build\exe\a.out.exe: The process cannot access the file because it is being used

    goland中build一直提示 open C:\Users\ \AppData\Local\Temp\go-build2905132708\b001\exe\a.out.exe: The proce ...

  6. Winform中使用FileStream读取文件后,继续操作提示:it is being used by anothor process

    场景 使用Winform搭建FTP客户端之后,读取本地某路径下的文件,然后将文件读取进行上传,再删除,在进行删除时提示: System.IO.IOException:The process cannn ...

  7. kvm静态迁移error: Cannot access storage file

    kvm静态迁移小坑, 1,kvm静态迁移virsh define  /etc/libvirt/qemu/xxxxxxxx.xml 后 执行virsh start  xxxxxx  报出下面的错误 er ...

  8. ERROR: Unable to launch the RTL Schematic process. The input NGR file, Can not fnd the module的解决方法

    我今天的verilog程序语法检测已通过,在综合时先后如下错误提示: 错误1: "ERROR: Unable to launch the RTL Schematic process. The ...

  9. Failed to execute child process duplicity (No such file or directory)错误解决

    恢复文件时报Failed to execute child process "duplicity" (No such file or directory)错误 尝试在命令行执行su ...

  10. Unable to access jar file xxx.jar

    最近在编写自动重启某个jar包的shell脚本的时候,老是报错"unable to access jarfile ***.jar "--不能访问jar包,奇怪啊,怎么会找不到jar ...

最新文章

  1. 制作模板_木模板制作流程
  2. Asp.net内置对象之Cookies
  3. 阶段1 语言基础+高级_1-2 -面向对象和封装_9两个引用指向同一个对象空间
  4. 赚大钱必备 怎样成为赚钱高手(图)
  5. 计算机网络学习笔记(七)——传输层、TCP三次握手、四次挥手、TCP流量控制、大总结
  6. 三星Galaxy Tab S8系列国行版曝光:对标iPad Pro的旗舰平板
  7. ES6中字符串新增方法
  8. 柔和渐变UI素材,让设计更加柔和的法宝。
  9. Papervision3D对象
  10. SQL Server 中的 JSON 数据
  11. WordPress百度熊掌号页面改造(纯代码实现)
  12. 【转载】Python tips: 什么是*args和**kwargs?
  13. uboot驱动模型(DM)分析(二)
  14. QQ第三方登录的应用
  15. 安卓设备门禁识别开发_基于android手机的视频通话门禁控制系统
  16. 测风塔选址与代表性问题
  17. 用全站 CDN 部署 Discourse 论坛
  18. 解决mysql不能修改密码_如何解决mysql修改密码后不能成功登录_数据库_mysql修改密码_课课家...
  19. Scala语言学习一——基础语法
  20. 小学生 学计算机通信 知识,小学通信知识ppt

热门文章

  1. 在 Mac 上的 Keynote 中如何先排版再选照片?
  2. EazyDraw for Mac(矢量图绘制软件)
  3. Cornerstone 4 for Mac(SVN管理工具)
  4. 使用VLC Media Player 在Mac上修剪视频
  5. 如何使用Xilisoft Video Editor将多个视频文件合并到一个文件中?
  6. OA系统中的HRM的发展和存在的误区,值得每一个HR学习
  7. 常见异常与错误 翻译
  8. Greenplu数据库的部署
  9. 2017.11.24 Stm8L151-factory
  10. 什么时候应该避免注释代码?