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

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. Linux内核学习资料
  2. python cookbook pdf下载-Python Cookbook 第3版 中文版.pdf
  3. python面试-Python面试常问的10个问题
  4. 技术向:一文读懂卷积神经网络CNN
  5. java判断点与线与面的关系_VC++开发GIS系统(280)判断点与面的拓扑关系
  6. qpython3使用手册图_qpython图形
  7. homebrew install php53
  8. Angular CLI的简单使用(1)
  9. linux开启ssh服务,实现ssh远程登录
  10. 记一次参加 CrossOver Meetup 的经历
  11. Object.assign()解释整理
  12. git 取消 所有暂存_Git版本管理完全指南—学好Git一文足矣
  13. Spark的RDD检查点实现分析
  14. TELERIK Reporting 实践
  15. java实验报告_java实验报告完整版.pdf
  16. 安装 VS 2015 报错 kb2999226
  17. 问题解决模型ORID
  18. VUE使用benz-amr-recorder 实现解码、播放
  19. 自动驾驶中图像与点云融合的深度学习研究综述
  20. 第七届万向区块链全球峰会 | 邹传伟:元宇宙经济学

热门文章

  1. 苹果Mac数据恢复工具:​​​​​​​​Disk Drill Enterprise
  2. 苹果Mac更改备忘录默认字体的方法图解
  3. 多功能笔记工具Notability创建甜甜圈图案的趣味教程
  4. DigitalRealty公司在达拉斯新建一个数据中心
  5. 使用Ubuntu的Crontab定时任务需要注意的地方
  6. nginx中配置虚拟主机
  7. FileSync文件同步更新工具
  8. Asp.net MVP模式介绍
  9. iOS 使用SourceTree注意事项
  10. Android 拷贝Asset目录下文件或者文件夹