Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

c# - The process cannot access the file because it is being used by another process

I'm trying to read a log file of log4net:

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)

and I get the Exception specified on the topic. I guess log4Net is holdin an exclusive lock on the file, but, as for example Notepad++ can read the file, I guess is technically possible to do this.

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
using (FileStream fs = 
    new FileStream(filePath,
        FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
//...

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

Your log may be write locked, so try with FileShare.ReadWrite.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...