473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

OPEN A FILE FOR READ ONLY

I want to open the current IIS log file in read-only mode. This file is held
by IIS but I still can open it in Notepad, but I have not been able (so far)
to read from it using my VB.NET program.

I have tried:
Dim fileReader As StreamReader
Try
fileReader = My.Computer.FileSystem.OpenTextFileReader(argument s(1) &
"\ex" & LogDate & ".log")
Catch ex As .....

And this throws an exception when the file is already in use by IIS.

What can I do to make this work?

Regards

Morten Fagermoen

Oct 13 '06 #1
3 18756
Morten Fagermoen wrote:
I want to open the current IIS log file in read-only mode. This file is held
by IIS but I still can open it in Notepad, but I have not been able (so far)
to read from it using my VB.NET program.

I have tried:
Dim fileReader As StreamReader
Try
fileReader = My.Computer.FileSystem.OpenTextFileReader(argument s(1) &
"\ex" & LogDate & ".log")
Catch ex As .....

And this throws an exception when the file is already in use by IIS.

What can I do to make this work?
You need to use a method that allows you to set the FileSharing mode.
Try a FileStream in connunction with a StreamReader:

Dim fs As New FileStream("c:\test\filename.ext", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)

Dim sr As New StreamReader(fs)

'Use the stream reader here

sr.Close()

fs.Close()
Hope this helps

Oct 13 '06 #2
Thank you, Chris!

This tip made my day!!

Morten
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Morten Fagermoen wrote:
>I want to open the current IIS log file in read-only mode. This file is
held
by IIS but I still can open it in Notepad, but I have not been able (so
far)
to read from it using my VB.NET program.

I have tried:
Dim fileReader As StreamReader
Try
fileReader = My.Computer.FileSystem.OpenTextFileReader(argument s(1) &
"\ex" & LogDate & ".log")
Catch ex As .....

And this throws an exception when the file is already in use by IIS.

What can I do to make this work?

You need to use a method that allows you to set the FileSharing mode.
Try a FileStream in connunction with a StreamReader:

Dim fs As New FileStream("c:\test\filename.ext", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)

Dim sr As New StreamReader(fs)

'Use the stream reader here

sr.Close()

fs.Close()
Hope this helps
Oct 13 '06 #3
"Morten Fagermoen" <mo****@fagermoen.comschrieb:
This tip made my day!!
Little explanation: The parameter of 'FileShare' does not specify which
rights your application requires but instead it specifies which rights other
applications are granted after your application opens the file. That's why
'FileShare.ReadWrite' is the specified value.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Oct 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sara Khalatbari | last post by:
Dear friends In a code, I'm opening a file to read. Like : lines = open(filename).readlines() & I'm never closing it. I'm not writing in that file, I just read it. Will it cause any problems...
1
by: Matt | last post by:
I want to open a file (Excel spreadsheet) read-only using JavaScript. Can someone tell me if/how this can be done? Thanks in advance, Matt
3
by: Baldy | last post by:
Hi, I need to be able to open a form and allow users to change a drop down (that changes a subform) but doesn't allow them to change any data at all. They need to be able to change the drop down...
2
by: Filips Benoit | last post by:
Dear All, I know how to create a shortcut to open a mdb exclusively. Shortcuttarget = "path access.exe" "path access-file" /excl BUT what is te shorcuttarget for opening it read-only? Thanks,...
4
by: xenny | last post by:
Hi, I'm trying to see if a file (exportfile) is set to read only using File::GetAttributes: if ((File::GetAttributes(exportfile.c_str()) & FileAttributes::ReadOnly) != 0); {...
8
by: Karl | last post by:
No, not a cross-post. I posted to the wrong group first up. Hi all, Using Len(Dir$(txtFileName)) to check if a file exists, can anyone suggest an additional step to check the Read-Only...
1
by: sandy21380 | last post by:
I have code to write the results of an Excel file to pdf. I'd like to make that pdf 'read only' when saving. How do I do that? Thanks!
2
by: frank knuckles | last post by:
After exporting an Access table to Excel, I would like to make the Excel file Read-Only. Is it possible to do this with VBA from within Access? Thanks. FK
3
by: colemanj4 | last post by:
Hello, I have a database that is on my company's network that everyone needs to be able to see, but I only want certain people to change any data in the database. Is there a way (like there is...
13
by: AdilSaumtally | last post by:
I have a form that I have opened from a button to open in "acFormReadOnly" and i want to hide certain buttons on the form. Is this possible?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.