惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
月光博客
月光博客
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
TaoSecurity Blog
TaoSecurity Blog
V
Visual Studio Blog
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
罗磊的独立博客
雷峰网
雷峰网
T
Tor Project blog
L
LINUX DO - 最新话题
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
Scott Helme
Scott Helme
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
腾讯CDC
Cloudbric
Cloudbric
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
T
Troy Hunt's Blog
T
Threatpost
C
Check Point Blog
Vercel News
Vercel News
I
Intezer
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
D
DataBreaches.Net
SecWiki News
SecWiki News
Help Net Security
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
Hacker News: Ask HN
Hacker News: Ask HN
AI
AI
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 冯岩

RSA算法 Android JAVA C#互通 jquery.autocomplete.js 插件的自定义搜索规则 LINQ TO SQL 动态查询 ASPX多服务器控件下使用Jquery.validate.js 接受来自服务器的数据连接时发生超时(30000 毫秒)问题原因及解决方法 防止网页被客户端IE缓存 dottext阅读之系统调度分析 Javascript对日期的操作 SqlServer 无日志文件附加 windows2003远程桌面退出后系统自动注销的解决方法 JS获取触发事件元素的方法 JS处理选取值 Marquee首尾相连不间断移动 开始完全显示 c#将数据导入Excel另类方法 windows Server 2008常见问题及解决方法 .NET 特性Attribute[三] .NET 特性Attribute[二] .NET 特性Attribute[一] [安装程序配置服务器失败]解决SQL Server2000安装失败
C#读取被进程占用的文件
冯岩 · 2009-02-04 · via 博客园 - 冯岩

文件“D:\Log\Cargoabc\logfilecargoabc.txt”正由另一进程使用,因此该进程无法访问该文件。

logfilecargoabc.txt是一个日志文件,不定时都可能由另外的程序对它进行日志记录写入操作。

今需要对日志文件读取出来,显示在日志查询里,需要用到了IO流。

1、 FileStream fs = File.OpenRead(url);
StreamReader sr = new StreamReader((System.IO.Stream)fs, System.Text.Encoding.Default);

错误提示:文件“D:\Log\Cargoabc\logfilecargoabc.txt”正由另一进程使用,因此该进程无法访问该文件。

2、StreamReader sr = File.OpenText(url);

错误提示:错误提示:文件“D:\Log\Cargoabc\logfilecargoabc.txt”正由另一进程使用,因此该进程无法访问该文件。

3、 FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);

正确读取。

总结:

这样的情况,不单要与只读方式打开txt文件,而且,需要共享锁。还必须要选择flieShare方式为ReadWrite。因为随时有其他程序对其进行写操作。