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

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.

FileStream:The process cannot access the file because it is being used by another process的更多相关文章

  1. C# The process cannot access the file because it is being used by another process

    C# The process cannot access the file because it is being used by another process   The process cann ...

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

    当在IIS中改动绑定的port号后启动时遇到例如以下错误,表明你的port号已经被占用了 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdmljMDIyOA ...

  3. Go-The process cannot access the file because it is being used by another process.

    where? Go程序在读取文件时候 why? 因为有其他进程也在读取和Go程序想要读取的文件,参数冲突 way? 关闭其他程序进程对该文件的读取操作

  4. Solution: The process cannot access the file [filename] because it is being used by another process.

    http://www.brianstevenson.com/blog/solution-the-process-cannot-access-the-file-filename-because-it-i ...

  5. 自定义容器启动脚本报错:exec user process caused "no such file or directory"

    创建容器起不来,一直是restarting状态,查看容器的报错日志如下: standard_init_linux.go:178: exec user process caused "no s ...

  6. HDFS relaxes a few POSIX requirements to enable streaming access to file system data

    https://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction [ ...

  7. C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、directoryInfo、Path、Encoding)

    (FileStream.StreamWriter .StreamReader .File.FileInfo.Directory.DirectoryInfo.Path.Encoding)     C#文 ...

  8. use tomcat to access the file cross the environment

    background: 项目中的一个小工具,是一个Cron Job ,每天去搜集下服务器Hadoop Job的运行状态,并生成一份报告发送给整个Team,生产报告的同时把相关的日志文件保存到固定的一台 ...

  9. Microsoft Office Excel cannot access the file, There are several possible reasons

    今天在做EXCEL打印读取模板时报错了,错误信息如下: Microsoft Excel cannot access the file 'D:\xx.xlsx'. There are several p ...

随机推荐

  1. 广东省-IT公司红黑榜排名

    红榜Top100 Order Company Name Point Change  1 百富计算机技术(深圳)有限公司  94.00 --  2 中国网通广州分公司  88.00 --  3 深圳市汇 ...

  2. ubuntu install mysql server method

         recently try to install mysql in my computer so that  I can practise some sql statement on seve ...

  3. sprintf,多少钱你知道?

    选<CSDN 社区电子杂志——C/C++杂志>http://emag.csdn.net 2005 年1 月 总号1 期 - 93 -笔者:steedhorse(晨星)printf 可能是很 ...

  4. 怎样处理iOS 5与iOS 6的 low-memory

    移动设备终端的内存极为有限,应用程序必须做好low-memory处理工作,才能避免程序因内存使用过大而崩溃. low-memory 处理思路 通常一个应用程序会包含多个view controllers ...

  5. WebService-03-使用CXF开发服务端和客户端

    写在前面的话 前面两节说了使用Java提供的包开发服务端和客户端,现在使用CXF来开发,CXF提供了两个类发而服务,一个是ServerFactoryBean,另一个是JaxWsServerFactor ...

  6. Android开发者必须深入学习的10个应用开源项目

    Android 开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项 目,贡献出他们的智慧和创造力.学习开源代码是掌握技术的 ...

  7. [置顶] iframe使用总结(实战)

    说在前面的话,iframe是可以做很多事情的. 例如: a>通过iframe实现跨域; b>使用iframe解决IE6下select遮挡不住的问题 c>通过iframe解决Ajax的 ...

  8. 【ALearning】第二章 Androidproject知识介绍

    本章介绍了主要的初步Androidproject成立了一个开发环境.为了Android意识的整体项目和理解.本章包含Android开发环境的搭建.第一Android工程Hello World与Andr ...

  9. oschina 建站系统

    建站系统 分类网站程序(9) 众筹平台(2) 团购网站系统(14) 开源轻博客系统(8) 开源博客系统(279) 视频网站系统(9) 开源微博工具(93) 论坛系统BBS(129) 建站系统CMS(5 ...

  10. Ajaxterm-0.10-8.el5.noarch.rpm CentOS 5 (RHEL 5) Download

    Ajaxterm-0.10-8.el5.noarch.rpm CentOS 5 (RHEL 5) Download Install Howto Download the latest epel-rel ...