opening a allready opened file (pfirewall.log)

O

Omko H

I am trying to open %windir%\pfirewall.log.
and I just can't get it to work (I am a absolute newbie to VB.net)

the strange thing is that notepad is perfectly able to view the file. but my
code keeps failing.

amongst other things i tried:
Dim s2 As New System.IO.FileStream(FileName, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read)

I keep getting this error when i try to open it:

An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
"C:\WINDOWS\pfirewall.log" because it is being used by another process.


does anyone know how i can open this file allthough it is locked by the
firewall?

Omko Huizenga
 
C

Chris Dunaway

The key is in the FileShare parameter you used. When you pass
FileShare.Read, you are saying: "Open the file such that other
programs can only read from the file while I am using it". Since the
firewall has already opened the file to write to it, your FileShare
mode fails. Try using the FileShare.ReadWrite value instead and see
if that helps.
 
O

Omko H

thanks a lot! it works.
but i don't realy get it. does this mean that when i open a file that is
allready opened, I have to match the type of lock that is on that particular
file??
Omko
 
C

Chris Dunaway

You don't have to match it, but whatever lock you use must not be
contrary to the lock used by the other program.

For example, if the other program had opened the file using
FileShare.None, then you would not be able to open the file at all.

FileShare.None means "Open the file and don't allow any other program
to open it for any reason."

FileShare.Read means "Open the file and only allow other programs to
read from the file but not write to it"

FileShare.Write means "Open the file and only allow other programs to
write the file, but not read it."

FileShare.ReadWrite means "Open the file and all other programs to read
from and write to the file."

In general, whoever opens the file first has control over how other
programs can access the file. If the firewall had not allowed read
access, you would not have been able to open the file for reading.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top