文件只读访问,不论锁(C#) [英] File Read-only access irrespective of locks (C#)

查看:149
本文介绍了文件只读访问,不论锁(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何打开(使用C#)的文件已经打开(在MS Word,例如)?我想如果我打开文件进行读取访问例如

How do I open (using c#) a file that is already open (in MS Word, for instance)? I thought if I open the file for read access e.g.

FileStream f= new FileStream('filename', FileMode.Open, FileAccess.ReadWrite);



我应该会成功,但我得到一个异常:

I should succeed, but I get an exception:

的过程,因为它已被锁定无法访问文件
......

"the process cannot access the file because it is locked ..."

我知道必须有读取文件不管放在任何锁的,因为我可以使用Windows资源管理器复制文件或使用记事本等其他程序打开它的一种方式,尽管它是在Word中打开。

I know there must be a way to read the file irrespective of any locks placed on it, because I can use windows explorer to copy the file or open it using another program like Notepad, even while it is open in WORD.

然而,似乎没有一个文件的IO类在C#允许我做这件事。为什么呢?

However, it seems none of the File IO classes in C# allows me to do this. Why?

推荐答案

您想设置的FileAccess =读取和文件共享=读写。这里是一个很好的文章(有为什么的解释和说明):

You want to set FileAccess=Read and FileShare=ReadWrite. Here is a great article on this (along with an explanation of why):

http://coding.infoconex.com/post/2009/04/How-do-I -open-A-文件的,是使用中,在C.aspx

这篇关于文件只读访问,不论锁(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆