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

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. Android获取Activity(应用)的执行状态及其它信息

    检測某Activity是否在当前Task的栈顶 public static boolean isTopActivy(String cmdName, Context context) { Activit ...

  2. CentOS IP丢失,切换了网络连接导致的vmnet8未启用dhcp

    解决了, 这个问题是我在开启虚拟机ubuntu系统的过程中, 在主机win7上切换了网络连接导致的, 就是刚开始我用的无线宽带上网, 此时开启了ubuntu ,然后使用过程中,我在win7上切换回静态 ...

  3. [LeetCode]Swap Nodes in Pairs 成对交换

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  4. java的url 中国的争论导致了扭曲

    话不多说,,直接粘代码 发件人 UrlParaCode.jsp <%@ page language="java" import="java.util.*" ...

  5. Linux SSH端口转发

    SSH端口转发分为两种,一种是本地端口转发,又称为本地SSH隧道.一直是远程端口转发.SSH端口转发,还必须指定数据传送的目标主机,从而形成点对点的端口转发. 本地端口转发     假定有三台主机A. ...

  6. (转)一篇很不错的介绍Eclipse插件Menu及其扩展点的文章

    原文在:http://tech.ddvip.com/2010-04/1271054623150507.html 菜单是各种软件及开发平台会提供的必备功能,Eclipse 也不例外,提供了丰富的菜单,包 ...

  7. hdu2412(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2412 题意:给定一棵关系树 , 从中选择一些点 , 使这些点均不存在亲子关系 , 最多能取多少个点 , ...

  8. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  9. tomcat压缩优化和缓存策略

    tomcat压缩内容 tomcat的压缩优化就是将返回的html页面等内容经过压缩,压缩成gzip格式之后.发送给浏览器,浏览器在本地解压缩的过程. 对于页面量信息大或者带宽小的情况下用压缩方式还是蛮 ...

  10. windows之实现3D立体效果的三种方法

    第一种:快捷键:win+tab 另外一种:cmd输入rundll32.exe dwmapi #105 第三种:使用软件bumptop